Node.js 内置模块crypto使用事件方法(onreadable)加密的一些问题

javaScript代码以下:java


'use strict'; const crypto = require('crypto'); //实例化一个AES加密对象 const aesEncrept = crypto.createCipher('aes192', 'key');
aesEncrept.on(
'readable', () => { let data = aesEncrept.read(); console.log(1); console.log(data); if(data) { console.log(data.toString('hex')); } console.log(2); }); aesEncrept.on('end', () => { console.log(3); }); aesEncrept.write("Hello world!"); aesEncrept.end();

 

运行结果:ui

1
<Buffer ae 1b 3d 2c a1 56 18 e6 bd b0 30 d0 3d e9 82 b4>
ae1b3d2ca15618e6bdb030d03de982b4
2
1
null
2
3加密

 

使用事件加密比较有意思的是:spa

1.当数据加密完成能够读取的时候( 也就是readable事件触发的时候 ),readable事件会触发两次;3d

2.readable事件第一次触发的时候data数据是存在的;code

3.readable事件第二次触发的时候data数据会被置空对象

相关文章
相关标签/搜索