How to use the constants function from crypto

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

crypto.constants is a built-in module in Node.js that provides constant values for use with the crypto module.

26
27
28
29
30
31
32
33
34
35
}
// 创建加密算法
const rsapublicKeyEncode = function (data, publicKey) {
  let crypted = crypto.publicEncrypt({
    key: publicKey,
    padding: crypto.constants.RSA_PKCS1_PADDING
  }, Buffer.from(data)).toString('base64');
  return crypted;
};
var account = {
fork icon639
star icon209
watch icon12

+ 3 other calls in file

11
12
13
14
15
16
17
18
19
20
const pubc = cert.pub(key)
arr.forEach((_, i) => {
  const f = crypto.publicEncrypt(
    {
      key: pubc,
      padding: crypto.constants.RSA_PKCS1_PADDING, // RSA_PKCS1_OAEP_PADDING
    },
    buf.subarray(i * MAX_CHUNK_SIZE, i * MAX_CHUNK_SIZE + MAX_CHUNK_SIZE)
  )
  finalBuf = Buffer.concat([finalBuf, f])
fork icon62
star icon243
watch icon17

+ 7 other calls in file

How does crypto.constants work?

The crypto.constants module in Node.js provides constant values for use with the crypto module. These constants represent various cryptographic algorithms, cipher modes, and other parameters that are used in cryptography. The crypto.constants module contains a number of properties, including: crypto.constants.defaultCoreCipherList: A list of default cipher algorithms that can be used with the createCipher() and createDecipher() methods of the crypto module. crypto.constants.defaultCipherList: A list of default cipher algorithms that can be used with the createCipheriv() and createDecipheriv() methods of the crypto module. crypto.constants.sign: An object that contains constant values for signing algorithms, including RSA_PKCS1_SHA1, RSA_PKCS1_SHA256, RSA_PKCS1_SHA512, and others. crypto.constants.verify: An object that contains constant values for verifying algorithms, including RSA_PKCS1_SHA1, RSA_PKCS1_SHA256, RSA_PKCS1_SHA512, and others. These constant values can be used in conjunction with the methods of the crypto module to perform cryptographic operations, such as encrypting data, generating hashes, signing and verifying data, and more. Overall, crypto.constants provides a convenient way to access constant values that are commonly used in cryptography, making it easier to perform secure and reliable cryptographic operations in Node.js.

7
8
9
10
11
12
13
14
15
16
  'http://www.w3.org/2001/04/xmlenc#rsa-1_5',
  //https://csrc.nist.gov/News/2017/Update-to-Current-Use-and-Deprecation-of-TDEA
  'http://www.w3.org/2001/04/xmlenc#tripledes-cbc'];

function encryptKeyInfoWithScheme(symmetricKey, options, scheme, callback) {
  const padding = scheme === 'RSA-OAEP' ? crypto.constants.RSA_PKCS1_OAEP_PADDING : crypto.constants.RSA_PKCS1_PADDING;
  const symmetricKeyBuffer = Buffer.isBuffer(symmetricKey) ? symmetricKey : Buffer.from(symmetricKey, 'utf-8');

  try {
    var encrypted = crypto.publicEncrypt({
fork icon54
star icon35
watch icon88

+ 3 other calls in file

31
32
33
34
35
36
37
38
39
40

// set the options for encryption and decryption.
// do not include the keys, they'll be added 
// in the encrypt and decrypt functions:
let options = {
  padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
  oaepHash: 'sha256'
};

let inputText, fileName;
fork icon35
star icon30
watch icon4

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const crypto = require("crypto");

const algorithm = "aes-256-cbc";
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);

const plaintext = "This is a secret message!";
console.log(`Plaintext: ${plaintext}`);

// Create a cipher using the default core cipher list
const cipher = crypto.createCipher(algorithm, key, iv);

// Encrypt the plaintext
let ciphertext = cipher.update(plaintext, "utf8", "hex");
ciphertext += cipher.final("hex");
console.log(`Ciphertext: ${ciphertext}`);

// Create a decipher using the default core cipher list
const decipher = crypto.createDecipher(algorithm, key, iv);

// Decrypt the ciphertext
let decrypted = decipher.update(ciphertext, "hex", "utf8");
decrypted += decipher.final("utf8");
console.log(`Decrypted: ${decrypted}`);

In this example, we use crypto.constants.defaultCoreCipherList to create a cipher and decipher for encrypting and decrypting data using the Advanced Encryption Standard (AES) cipher in cipher-block chaining (CBC) mode. We start by generating a random key and initialization vector (IV) using the crypto.randomBytes() method. We then define a plaintext message and log it to the console. Next, we create a cipher object using the createCipher() method of the crypto module, passing in the algorithm, key, and iv as parameters. We use the cipher object to encrypt the plaintext message, storing the result in the ciphertext variable. We then create a decipher object using the createDecipher() method of the crypto module, passing in the same algorithm, key, and iv as parameters. We use the decipher object to decrypt the ciphertext and store the result in the decrypted variable. Finally, we log the decrypted message to the console. Overall, this example demonstrates how to use crypto.constants.defaultCoreCipherList to perform secure encryption and decryption of data in Node.js using the crypto module.

156
157
158
159
160
161
162
163
164
165
// 3-5. Attempt to decrypt using crypto lib
try {
  data = Buffer.from(data)
  result = crypto.privateDecrypt({
    key: key.handle, 
    padding: crypto.constants.RSA_PKCS1_OAEP_PADDING
  },
  data)
} catch (error) {
  throw new OperationError(error.message)
fork icon16
star icon0
watch icon3

+ 3 other calls in file

85
86
87
88
89
90
91
92
93
94
95
function _signSHA256RSA(value, privateKey) {
  // Converting value string to a buffer then
  //using sha256 - converting to hexa decimal encoded string
  const signature = crypto.sign("sha256", Buffer.from(value), {
    key: privateKey,
    padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
  });


  //Signature is created using sign function of crypto
  return signature;
fork icon0
star icon0
watch icon0

+ 7 other calls in file

12
13
14
15
16
17
18
19
20
21
22
}


const rsaEncrypt = (buffer, key) => {
  buffer = Buffer.concat([Buffer.alloc(128 - buffer.length), buffer])
  return crypto.publicEncrypt(
    { key: key, padding: crypto.constants.RSA_NO_PADDING },
    buffer,
  )
}

fork icon0
star icon0
watch icon0

153
154
155
156
157
158
159
160
161
162
// Maximum permissible salt length
const max = keySize / 8 - hLen - 2;

function getEffectiveSaltLength(saltLength) {
  switch (saltLength) {
    case crypto.constants.RSA_PSS_SALTLEN_DIGEST:
      return hLen;
    case crypto.constants.RSA_PSS_SALTLEN_MAX_SIGN:
      return max;
    default:
fork icon0
star icon0
watch icon0

+ 47 other calls in file

97
98
99
100
101
102
103
104
105
assert.strictEqual(decryptedBufferWithPassword.toString(), input);

// Now with RSA_NO_PADDING. Plaintext needs to match key size.
const plaintext = 'x'.repeat(rsaKeySize / 8);
encryptedBuffer = crypto.privateEncrypt({
  padding: crypto.constants.RSA_NO_PADDING,
  key: rsaKeyPemEncrypted,
  passphrase: bufferPassword
}, Buffer.from(plaintext));
fork icon0
star icon0
watch icon0

+ 29 other calls in file

46
47
48
49
50
51
52
53
54
55
56
 }
class TlsBuilder {
    constructor (socket){
        this.curve = "GREASE:X25519:x25519"; // Default
        this.sigalgs = SignalsList;
        this.Opt = crypto.constants.SSL_OP_NO_RENEGOTIATION|crypto.constants.SSL_OP_NO_TICKET|crypto.constants.SSL_OP_NO_SSLv2|crypto.constants.SSL_OP_NO_SSLv3|crypto.constants.SSL_OP_NO_COMPRESSION|crypto.constants.SSL_OP_NO_RENEGOTIATION|crypto.constants.SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION|crypto.constants.SSL_OP_TLSEXT_PADDING|crypto.constants.SSL_OP_ALL|crypto.constants.SSLcom;
    }


    Alert(){
    }
fork icon0
star icon0
watch icon0

170
171
172
173
174
175
176
177
178
179
    thing = normalizeInput(thing);
    var signer = crypto.createSign('RSA-SHA' + bits);
    var sig = (signer.update(thing), signer.sign({
      key: privateKey,
      padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
      saltLength: crypto.constants.RSA_PSS_SALTLEN_DIGEST
    }, 'base64'));
    return fromBase64(sig);
  }
}
fork icon0
star icon0
watch icon0

+ 7 other calls in file

52
53
54
55
56
57
58
59
60
61
var endOffSet = MAX_ENCRYPT_BLOCK;  //结束长度
//分段加密
while (inputLen - offSet > 0) {
  if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
    var bufTmp = bufferToEncrypt.slice(offSet, endOffSet);
    bufs.push(crypto.publicEncrypt({ key: publicKey, padding: crypto.constants.RSA_PKCS1_PADDING }, bufTmp));
  } else {
    var bufTmp = bufferToEncrypt.slice(offSet, inputLen);
    bufs.push(crypto.publicEncrypt({ key: publicKey, padding: crypto.constants.RSA_PKCS1_PADDING }, bufTmp));
  }
fork icon0
star icon0
watch icon2

+ 7 other calls in file

425
426
427
428
429
430
431
432
433
called. Multiple calls to `verify.verify()` will result in an error being
thrown.

## `crypto` module methods and properties

### crypto.constants
<!-- YAML
added: v6.3.0
-->
fork icon0
star icon0
watch icon1

9
10
11
12
13
14
15
16
17

console.log(decrypted.toString('utf8'))

// crypto.constants.RSA_PKCS1_PADDING
// crypto.constants.RSA_SSLV23_PADDING
// crypto.constants.RSA_NO_PADDING
// crypto.constants.RSA_PKCS1_OAEP_PADDING
// crypto.constants.RSA_X931_PADDING
// crypto.constants.RSA_PKCS1_PSS_PADDING
fork icon0
star icon0
watch icon1

+ 11 other calls in file