cipher.getAuthTag() 方法返回一個緩衝區,其中包含根據給定數據計算出的身份驗證標簽。該方法應該在使用final方法之後調用。
Buffer 對象用於表示固定長度的字節序列。
用法:
cipher.getAuthTag()
Parameters: 該方法不接受任何參數。
返回值:此方法返回一個緩衝區,其中包含根據給定數據計算出的身份驗證標記。
示例 1:在下麵的示例中,我們將在控製台屏幕上打印緩衝區。
Javascript
const crypto = require('crypto'),
algorithm = 'aes-256-gcm',
password = '3zTvzr3p67VC61jmV54rIYu1545x4TlY',
// Do not use a global iv for production,
// generate a new one for each encryption
iv = '60iP0h6vJoEa'
function encrypt(text) {
let cipher = crypto.createCipheriv(algorithm, password, iv)
let encrypted = cipher.update(text, 'utf8', 'hex')
encrypted += cipher.final('hex');
let tag = cipher.getAuthTag();
return {
content: encrypted,
tag: tag
};
}
let output = encrypt("GeeksforGeeks")
// Here, we are printing the tag and content.
// Content is Encrypted
console.log(output);
輸出:
{ content: '57e625f9675b265c32a86966eb', tag: <Buffer df e6 cc ff 9e 70 47 2e 66 4a f0 ba 08 53 17 b5> }
示例2:在下麵的示例中,我們將在最終方法之前調用getAuthTag()方法。
Javascript
const crypto = require('crypto'),
algorithm = 'aes-256-gcm',
password = '3zTvzr3p67VC61jmV54rIYu1545x4TlY',
// Do not use a global iv for production,
// generate a new one for each encryption
iv = '60iP0h6vJoEa'
function encrypt(text) {
let cipher = crypto.createCipheriv(algorithm, password, iv)
let encrypted = cipher.update(text, 'utf8', 'hex')
let tag = cipher.getAuthTag();
encrypted += cipher.final('hex');
return {
content: encrypted,
tag: tag
};
}
let output = encrypt("GeeksforGeeks")
// Here, we are printing the tag and content.
// Content is Encrypted
console.log(output);
輸出:在此示例中,我們將收到如下錯誤。
node:internal/crypto/cipher:213 throw new ERR_CRYPTO_INVALID_STATE('getAuthTag');
參考: https://nodejs.org/api/crypto.html#ciphergetauthtag
相關用法
- Node.js cipher.update()用法及代碼示例
- Node.js cipher.final()用法及代碼示例
- Node.js cipher.setAAD()用法及代碼示例
- Node.js cipher.setAutoPadding()用法及代碼示例
- Node.js crypto.createDiffieHellman()用法及代碼示例
- Node.js console.clear()用法及代碼示例
- Node.js console.time()用法及代碼示例
- Node.js console.timeEnd()用法及代碼示例
- Node.js console.log()用法及代碼示例
- Node.js console.error()用法及代碼示例
- Node.js console.warn()用法及代碼示例
- Node.js crypto.createSign()用法及代碼示例
- Node.js crypto.createVerify()用法及代碼示例
- Node.js crypto.getCiphers()用法及代碼示例
- Node.js crypto.getHashes()用法及代碼示例
- Node.js crypto.getCurves()用法及代碼示例
- Node.js crypto.getDiffieHellman()用法及代碼示例
- Node.js crypto.pbkdf2()用法及代碼示例
- Node.js crypto.createHash()用法及代碼示例
- Node.js crypto.createHmac()用法及代碼示例
- Node.js crypto.randomBytes()用法及代碼示例
- Node.js crypto.publicDecrypt()用法及代碼示例
- Node.js crypto.pbkdf2Sync()用法及代碼示例
- Node.js crypto.createECDH()用法及代碼示例
- Node.js crypto.createDecipheriv()用法及代碼示例
注:本文由純淨天空篩選整理自neeraj3304大神的英文原創作品 Node.js cipher.getAuthTag() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。