当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js cipher.final()用法及代码示例


cipher.final()方法是加密模块中的Cipher类的内置应用程序编程接口,用于返回包含cipher对象值的缓冲区。

用法:

const cipher.final([outputEncoding])

参数:此方法将输出编码作为参数。

返回值:此方法返回包含密码值的缓冲区的对象。

范例1: 文件名:index.js



Javascript

// Node.js program to demonstrate the 
// cipher.final() method 
  
// Importing crypto module 
const crypto = require('crypto'); 
  
// Creating and initializng algorith and password 
const algorithm = 'aes-192-cbc'; 
const password = 'Password used to generate key'; 
  
// Getting key for the cipher object 
const key = crypto.scryptSync(password, 'salt', 24); 
  
// Creating and initializg the static iv 
const iv = Buffer.alloc(16, 0); 
  
// Creating and initializng the cipher object 
const cipher = crypto.createCipheriv(algorithm, key, iv); 
  
// Getting buffer value  
// by using final() method 
let value = cipher.final('hex'); 
  
// Display the result 
console.log("buffer:- " + value);

输出:

buffer:- b9be42878310d599e4e49e040d1badb9

范例2: 文件名:index.js

Javascript

// Node.js program to demonstrate the 
// cipher.final() method 
  
// Importing crypto module 
const crypto = require('crypto'); 
  
// Creating and initializng algorith and password 
const algorithm = 'aes-192-cbc'; 
const password = 'Password used to generate key'; 
  
// Getting key for cipher object 
crypto.scrypt(password, 'salt', 24, 
    { N:512 }, (err, key) => { 
  
        if (err) throw err; 
  
        // Creating and initializg the static iv 
        const iv = Buffer.alloc(16, 0); 
  
        // Creating and initializng the cipher object 
        const cipher = crypto 
            .createCipheriv(algorithm, key, iv); 
  
        // Getting buffer value  
        // by using final() method 
        let value = cipher.final('hex'); 
  
        // Display the result 
        console.log("buffer:- " + value); 
    });

输出:

buffer:- 726cccfc7d80ca473d8d4de1a0a42675

使用以下命令运行index.js文件:

node index.js

参考:https://nodejs.org/dist/latest-v12.x/docs/api/crypto.html#crypto_cipher_final_outputencoding

相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Node.js cipher.final() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。