How to use the createPrivateKey function from crypto

Find comprehensive JavaScript crypto.createPrivateKey code examples handpicked from public code repositorys.

54
55
56
57
58
59
60
61
62
63
// Load the public and private keys
// The public one is used for encrypting the message (in this case, to encrypt a symmetric key) and the private one is using for decryption
const publicKeyObject = crypto.createPublicKey(
    await readFile('public.pem')
)
const privateKeyObject = crypto.createPrivateKey(
    await readFile('private.pem')
)

// Encrypt the message using hybrid encryption, and obtain both the ciphertext (encrypted message) and the wrapped key (encrypted key)
fork icon10
star icon28
watch icon7

153
154
155
156
157
158
159
160
161
162
* Serializes and signs the payload as a PASETO using the provided private key
* @example
* const { createPrivateKey } = require('crypto')
* const { V1 } = require('paseto')
*
* const key = createPrivateKey(privateKey)
*
* const payload = {
*   'urn:example:claim': 'foo'
* }
fork icon24
star icon1
watch icon1

+ 7 other calls in file

7
8
9
10
11
12
13
14
15
16

const key = HsArr.includes(config.algorithm)
  ? config.isBase64Encoded
    ? Buffer.from(config.Secret).toString('base64')
    : config.Secret
  : createPrivateKey(config.Secret)

const authorizationValue = jwt.sign(
  config.payload ? JSON.parse(config.payload) : {},
  key,
fork icon5
star icon8
watch icon0

107
108
109
110
111
112
113
114
115
116
117
}


const signMessage = (privateKey, passphrase, message) => {
    try {
        const key = trimPemKey(privateKey);
        const privateKeyToSign = crypto.createPrivateKey({
            key: key,
            passphrase: passphrase
        });
        const sign = crypto.createSign('SHA256');
fork icon0
star icon0
watch icon1

104
105
106
107
108
109
110
111
112
113
  return failure(new Error('secretOrPrivateKey must have a value'));
}

if (secretOrPrivateKey != null && !(secretOrPrivateKey instanceof KeyObject)) {
  try {
    secretOrPrivateKey = createPrivateKey(secretOrPrivateKey)
  } catch (_) {
    try {
      secretOrPrivateKey = createSecretKey(typeof secretOrPrivateKey === 'string' ? Buffer.from(secretOrPrivateKey) : secretOrPrivateKey)
    } catch (_) {
fork icon0
star icon0
watch icon1

+ 2 other calls in file

252
253
254
255
256
257
258
259
260
261

//create gRpc client to designated port
const client = await this._createClientForPeer(mspId, peer);

//create signer using the private key of the peer
const privateKey = crypto.createPrivateKey(walletIdentity.privateKey);
const signer = signers.newPrivateKeySigner(privateKey);

//create gateway instance using the grpc client, identity and signer
const gateway = connect({
fork icon0
star icon0
watch icon1

+ 3 other calls in file

78
79
80
81
82
83
84
85
86
87
if (!pubKey || pubKey.byteLength != 32) {
  throw new Error("Invalid public key");
}

if (typeof nodeCrypto.diffieHellman === "function") {
  const nodePrivateKey = nodeCrypto.createPrivateKey({
    key: Buffer.concat([PRIVATE_KEY_DER_PREFIX, privKey]),
    format: "der",
    type: "pkcs8",
  });
fork icon0
star icon0
watch icon1

56
57
58
59
60
61
62
63
64
65
66
       'iBJKZLQMqNdbY14G9rdKmhhTJrQjC+i7Q/wI8JPhOFzHIGA=\n' +
       '-----END PUBLIC KEY-----',
  format: 'pem'
});


const bobPrivateKey = crypto.createPrivateKey({
  key: '-----BEGIN PRIVATE KEY-----\n' +
       'MIIBoQIBADCB1QYJKoZIhvcNAQMBMIHHAoHBAP//////////yQ/aoiFowjTExmKL\n' +
       'gNwc0SkCTgiKZ8x0Agu+pjsTmyJRSgh5jjQE3e+VGbPNOkMbMCsKbfJfFDdP4TVt\n' +
       'bVHCReSFtXZiXn7G9ExC6aY37WsL/1y29Aa37e44a/taiZ+lrp8kEXxLH+ZJKGZR\n' +
fork icon0
star icon0
watch icon0

+ 23 other calls in file

450
451
452
453
454
455
456
457
458
459
                     true);
}

{
  const data = Buffer.from('Hello world');
  const privKeyObj = crypto.createPrivateKey(pair.private);
  const pubKeyObj = crypto.createPublicKey(pair.public);

  const sig = crypto.sign(algo, data, privKeyObj);
  assert.strictEqual(sig.length, pair.sigLen);
fork icon0
star icon0
watch icon0

372
373
374
375
376
377
378
379
380
381
382
383
})().then(common.mustCall());


{
  const rsaPublic = crypto.createPublicKey(
    fixtures.readKey('rsa_public_2048.pem'));
  const rsaPrivate = crypto.createPrivateKey(
    fixtures.readKey('rsa_private_2048.pem'));


  for (const [name, publicUsages, privateUsages] of [
    ['ECDSA', ['verify'], ['sign']],
fork icon0
star icon0
watch icon0

532
533
534
535
536
537
538
539
540
541
542
543
})().then(common.mustCall());


{
  const ecPublic = crypto.createPublicKey(
    fixtures.readKey('ec_p256_public.pem'));
  const ecPrivate = crypto.createPrivateKey(
    fixtures.readKey('ec_p256_private.pem'));


  for (const [name, [publicUsage, privateUsage]] of Object.entries({
    'RSA-PSS': ['verify', 'sign'],
fork icon0
star icon0
watch icon0

0
1
2
3
4
5
6
7
8
const crypto = require('crypto')
const fs = require('fs')

const encrypted = Buffer.from(fs.readFileSync('protected.txt', 'utf8'), 'base64')

const privateKey = crypto.createPrivateKey(fs.readFileSync('private.pem', 'utf8'))

const decrypted = crypto.privateDecrypt({key: privateKey, padding: crypto.constants.RSA_PKCS1_PADDING}, Buffer.from(encrypted, 'base64'))
// const decrypted = crypto.privateDecrypt(privateKey, Buffer.from(encrypted, 'base64'))
fork icon0
star icon0
watch icon1