admin管理员组文章数量:1401595
I create a cipher passing two buffers. buf1 is they key, a 32 bytes buffer, and buf2, which is the IV, is also a 32 bytes buffer that I slice to only use 16 bytes. Documentation says that cipher.update and cipher.final return a buffer when nothing is specified. That is actually what I wish to happen. While I guess I can just do a new Buffer(crypted, 'binary') to convert it, I'm wondering if I might be doing something wrong.
> var cipher = crypto.createCipheriv('aes-256-cbc', buf1, buf2.slice(0,16));
undefined
> var crypted = cipher.update(new Buffer('this is some test'));
undefined
> crypted += cipher.final();
'!t\u001f\u0004>.\u0012\u0001���K\u001bSiA�]3\u0017�6�&�.��\u0015�V?'
0> Buffer.isBuffer(crypted)
false
.html#crypto_class_cipher
I'm using node.js version 0.10.10, which is latest stable and supposedly matches the documentation linked:
$ node -v
v0.10.10
Is this a documentation bug or a mistake from my part? I know that with v0.8 pbkdf2 returned a binary string instead of a buffer and now with 0.10.10 it returns a buffer as stated by the docs. I was hoping for cipher to also work it all with buffers instead of using binary... for consistency.
I create a cipher passing two buffers. buf1 is they key, a 32 bytes buffer, and buf2, which is the IV, is also a 32 bytes buffer that I slice to only use 16 bytes. Documentation says that cipher.update and cipher.final return a buffer when nothing is specified. That is actually what I wish to happen. While I guess I can just do a new Buffer(crypted, 'binary') to convert it, I'm wondering if I might be doing something wrong.
> var cipher = crypto.createCipheriv('aes-256-cbc', buf1, buf2.slice(0,16));
undefined
> var crypted = cipher.update(new Buffer('this is some test'));
undefined
> crypted += cipher.final();
'!t\u001f\u0004>.\u0012\u0001���K\u001bSiA�]3\u0017�6�&�.��\u0015�V?'
0> Buffer.isBuffer(crypted)
false
http://nodejs/api/crypto.html#crypto_class_cipher
I'm using node.js version 0.10.10, which is latest stable and supposedly matches the documentation linked:
$ node -v
v0.10.10
Is this a documentation bug or a mistake from my part? I know that with v0.8 pbkdf2 returned a binary string instead of a buffer and now with 0.10.10 it returns a buffer as stated by the docs. I was hoping for cipher to also work it all with buffers instead of using binary... for consistency.
Share Improve this question asked Jun 7, 2013 at 18:48 MamsaacMamsaac 6,2733 gold badges23 silver badges30 bronze badges1 Answer
Reset to default 8You're concatenating them using +=
which turns buffers into strings. Your code should be something more like this :
var cipher = crypto.createCipheriv('aes-256-cbc', buf1, buf2.slice(0,16));
var cryptedBuffers = [cipher.update(new Buffer('this is some test'))];
cryptedBuffers.push(cipher.final());
// at this point `cryptedBuffers` is an array of buffers which you can turn
// into a single buffer by doing
var crypted = Buffer.concat(cryptedBuffers);
本文标签: javascriptcryptocreateCipheriv gt cipherupdatecipherfinal does not return a BufferStack Overflow
版权声明:本文标题:javascript - crypto.createCipheriv -> cipher.update + cipher.final does not return a Buffer? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744243659a2596906.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论