How to use the createSign function from crypto
Find comprehensive JavaScript crypto.createSign code examples handpicked from public code repositorys.
crypto.createSign is a method in the Node.js crypto module that creates a cryptographic signing object, which can be used to sign data using a private key and a chosen algorithm.
GitHub: hokaccha/node-jwt-simple
178 179 180 181 182 183 184 185 186 187
var base64str; if(type === "hmac") { base64str = crypto.createHmac(method, key).update(input).digest('base64'); } else if(type == "sign") { base64str = crypto.createSign(method).update(input).sign(key, 'base64'); } else { throw new Error('Algorithm type not recognized'); }
+ 7 other calls in file
16 17 18 19 20 21 22 23 24 25
* @param {string} outputEncoding The encoding of the output signature. If provided a `string` is returned, otherwise a `Buffer` is returned. * @returns {string|Buffer} The generated signature for the provided message. * @static */ function sign(privateKey, algorithm, message, inputEncoding, outputEncoding) { const signFunc = crypto.createSign(algorithm); signFunc.update(message, inputEncoding); signFunc.end(); return signFunc.sign(privateKey, outputEncoding); }
+ 3 other calls in file
How does crypto.createSign work?
crypto.createSign is a method in the Node.js crypto module that creates a cryptographic signing object, which can be used to sign data using a private key and a chosen algorithm. When you call crypto.createSign(algorithm), the function returns a Sign object that can be used to sign data using the specified algorithm. Supported algorithms include "RSA-SHA256", "RSA-SHA512", "DSA-SHA", and "ECDSA-SHA". To sign data using the Sign object, you first call update(data) to add the data to be signed. You can call update(data) multiple times to add additional data, if needed. When you're ready to sign the data, you call sign(privateKey, [outputFormat]), passing in the private key to be used for signing. The function returns a buffer containing the signature. In essence, crypto.createSign provides a way to sign data using a private key and a chosen cryptographic algorithm, which can be useful for verifying the authenticity and integrity of data in a secure way. However, it is important to protect the private key and ensure that it is used securely to prevent unauthorized access or tampering.
GitHub: nwjs/node
255 256 257 258 259 260 261 262 263 264
// instead of // -----BEGIN RSA PRIVATE KEY----- and -----END RSA PRIVATE KEY----- const sha1_privateKey = fixtures.readKey('rsa_private_pkcs8_bad.pem', 'ascii'); // This would inject errors onto OpenSSL's error stack crypto.createSign('sha1').sign(sha1_privateKey); }, (err) => { // Do the standard checks, but then do some custom checks afterwards. assert.throws(() => { throw err; }, { message: 'error:0D0680A8:asn1 encoding routines:asn1_check_tlen:' +
+ 3 other calls in file
38 39 40 41 42 43 44 45 46 47 48 49 50
const privateKeyStr = fs.readFileSync(path.join(__dirname, './keys/prv.key'), inputCharset); console.log('privateKeyStr = ' + privateKeyStr); console.log(''); const sign = crypto.createSign(signAlgorithm).update(waitForSignStr, inputCharset).sign(fillPrivateKeyMarker(privateKeyStr), 'base64'); console.log('sign = ' + sign); console.log(''); // 验签
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
const crypto = require("crypto"); const privateKey = `-----BEGIN PRIVATE KEY----- MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC1BQOhn08IeOgB ... -----END PRIVATE KEY-----`; const data = "The quick brown fox jumps over the lazy dog"; const sign = crypto.createSign("RSA-SHA256"); sign.update(data); const signature = sign.sign(privateKey, "hex"); console.log(`Data: ${data}`); console.log(`Signature: ${signature}`);
In this example, we first import the crypto module. We then define a private key string, which will be used for signing the data. We then define some data to be signed: 'The quick brown fox jumps over the lazy dog'. We then call crypto.createSign('RSA-SHA256') to create a Sign object using the RSA-SHA256 algorithm. We then call sign.update(data) to add the data to be signed to the Sign object. We then call sign.sign(privateKey, 'hex') to sign the data using the private key and return a buffer containing the signature in hexadecimal format. Finally, we log the original data and the signature to the console. Note that in a real-world scenario, you would need to keep the private key secure and protect it from unauthorized access or tampering. This example is for demonstration purposes only.
GitHub: luckykiet/gastropay
111 112 113 114 115 116 117 118 119 120
const key = trimPemKey(privateKey); const privateKeyToSign = crypto.createPrivateKey({ key: key, passphrase: passphrase }); const sign = crypto.createSign('SHA256'); sign.update(message); return sign.sign(privateKeyToSign, 'base64'); } catch (error) { console.log(error)
43 44 45 46 47 48 49 50 51 52
var canonicalizedHeaders = Object.keys(basicRequest.headers).filter(function (item) { return item.startsWith('x-bm-'); }).sort().map(function (item, index, array) { return item + ':' + basicRequest.headers[item]; }).join("\n"); var hash = crypto.createSign('RSA-SHA256'); var toSign = [ basicRequest.method, (basicRequest.headers['content-md5'] || ''), (basicRequest.headers['content-type'] || ''),
134 135 136 137 138 139 140 141 142 143
const urlinfo = new URL(endpoint) const date = (new Date()).toUTCString() const signed_string = `(request-target): post /inbox\nhost: ${urlinfo.hostname}\ndate: ${date}\ndigest: SHA-256=${digestHash}` const signer = crypto.createSign('sha256'); signer.update(signed_string); signer.end(); const signature = signer.sign(privateKey); const header = `keyId="https://abrajam.com/actor/abraham",headers="(request-target) host date digest",signature="${signature.toString('base64')}"`
+ 4 other calls in file
322 323 324 325 326 327 328 329 330 331
] }; // Self-sign the certificate. const tbsDer = rfc5280.TBSCertificate.encode(tbs, 'der'); const signature = crypto.createSign(digest).update(tbsDer).sign(privateKey); // Construct the signed certificate. const cert = { tbsCertificate: tbs,
+ 2 other calls in file
37 38 39 40 41 42 43 44 45 46
} }; Object.defineProperty(Object.prototype, 'library', library); assert.throws(() => { crypto.createSign('sha1').sign( `-----BEGIN RSA PRIVATE KEY----- AAAAAAAAAAAA -----END RSA PRIVATE KEY-----`); }, { message: 'bye, bye, library' });
+ 35 other calls in file
145 146 147 148 149 150 151 152 153 154
return function sign(thing, privateKey) { checkIsPrivateKey(privateKey); thing = normalizeInput(thing); // Even though we are specifying "RSA" here, this works with ECDSA // keys as well. var signer = crypto.createSign('RSA-SHA' + bits); var sig = (signer.update(thing), signer.sign(privateKey, 'base64')); return fromBase64(sig); } }
+ 3 other calls in file
425 426 427 428 429 430 431 432 433 434 435
'-----BEGIN EC PRIVATE KEY-----\n' + 'MHcCAQEEIF+jnWY1D5kbVYDNvxxo/Y+ku2uJPDwS0r/VuPZQrjjVoAoGCCqGSM49\n' + 'AwEHoUQDQgAEurOxfSxmqIRYzJVagdZfMMSjRNNhB8i3mXyIMq704m2m52FdfKZ2\n' + 'pQhByd5eyj3lgZ7m7jbchtdgyOF8Io/1ng==\n' + '-----END EC PRIVATE KEY-----'; crypto.createSign('SHA256').sign(ecPrivateKey); } // Invalid test: curve argument is undefined assert.throws(
+ 6 other calls in file
614 615 616 617 618 619 620 621 622
'4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF'; crypto.createDiffieHellman(p, 'hex'); // Test RSA key signing/verification const rsaSign = crypto.createSign('SHA1'); const rsaVerify = crypto.createVerify('SHA1'); assert.ok(rsaSign instanceof crypto.Sign); assert.ok(rsaVerify instanceof crypto.Verify);
+ 11 other calls in file
248 249 250 251 252 253 254 255 256 257 258
}); } } // Test RSA key signing/verification let rsaSign = crypto.createSign('SHA1'); let rsaVerify = crypto.createVerify('SHA1'); assert.ok(rsaSign); assert.ok(rsaVerify);
+ 65 other calls in file
108 109 110 111 112 113 114 115 116 117
'MHcCAQEEIF+jnWY1D5kbVYDNvxxo/Y+ku2uJPDwS0r/VuPZQrjjVoAoGCCqGSM49\n' + 'AwEHoUQDQgAEurOxfSxmqIRYzJVagdZfMMSjRNNhB8i3mXyIMq704m2m52FdfKZ2\n' + 'pQhByd5eyj3lgZ7m7jbchtdgyOF8Io/1ng==\n' + '-----END EC PRIVATE KEY-----'; const sign = createSign('sha256').update('plaintext'); // TODO(bnoordhuis) This should really bubble up the specific OpenSSL error // rather than Node's generic error message. const badKey = 'f'.repeat(128);
19 20 21 22 23 24 25 26 27 28 29
}) router.post('/', async (req, res) => { //submit one new posts console.log("received") console.log(req.body) let signer = crypto.createSign(signing_algorithm) signer.update(req.body.title) signer.update(req.body.description) signer.end() let signature = signer.sign(private_Key,format_string)
0 1 2 3 4 5 6 7 8 9 10
const crypto = require("crypto"); const config = require("../config/config"); const pmlib = require("./sign-util-lib"); function test2() { const sign = crypto.createSign("RSA-SHA256"); sign.update("hello world"); sign.end(); let res = sign.sign(config.privateKey).toString("base64"); console.log(res);
149 150 151 152 153 154 155 156 157
Example: Using `Sign` objects as streams: ```js const crypto = require('crypto'); const sign = crypto.createSign('rsa-sha256'); sign.write('some data to sign'); sign.end();
+ 3 other calls in file
26 27 28 29 30 31 32 33 34 35
* @param privateKey * @returns {string} */ function createSign(data, privateKey) { // 'RSA-SHA1'--签名算法的名称 const signer = crypto.createSign('RSA-SHA1'); signer.update(data); signer.end(); return signer.sign(privateKey, 'base64') }
+ 3 other calls in file
247 248 249 250 251 252 253 254 255 256
Example: signing using legacy signature algorithm name ```js const crypto = require('crypto'); const sign = crypto.createSign('RSA-SHA256'); sign.update('some data to sign'); const privateKey = getPrivateKeySomehow();
+ 5 other calls in file
crypto.createHash is the most popular function in crypto (882 examples)