calvinmetcalf / chacha20poly1305

chacha20/poly1305 with the node api
40 stars 7 forks source link

Are there any examples of this? #8

Open pogland opened 8 years ago

pogland commented 8 years ago

I am stumped on how to use this. I really want to use ChaCha20 plus Poly1305 but it is not very popular so I couldn't find any sources on ChaCha20. This is the only source I have found. But I don't understand it very well. Could you put something like example.html in the Code area?

calvinmetcalf commented 8 years ago

it is designed for use with browserify, or in node.js, if you need something standalone you can use wzrd.in

h2y commented 7 years ago

same to me, I think the package needs a demo to encode and decode a buffer.

h2y commented 7 years ago

These code can't work as expected. T.T

console.log(originBuffer.toString());

cipher.setAAD(originBuffer);
let cipherRet = cipher.update(originBuffer, 'binary', 'hex');
    cipherRet += cipher.final('hex');

var tag = cipher.getAuthTag();

decipher.setAAD(originBuffer);
decipher.setAuthTag(tag);
decipher.update(cipherRet, 'hex', 'binary');
let out = decipher.final('binary');

console.log('out: ', out.toString());
calvinmetcalf commented 7 years ago

if you just want to know how to use buffers you can see these node references unless I'm misunderstanding

h2y commented 7 years ago

thanks for help, I'd like to use this module like another cipher in crypto. but the useage is not same. so i don't know how to do.

calvinmetcalf commented 7 years ago

the usage is identical to aes-gcm, what exactly is the issue you are having?

calvinmetcalf commented 7 years ago

ok I see the error in your code, binary doesn't do what you think it does and you were listening for the wrong thing a couple places.

console.log(originBuffer.toString());

cipher.setAAD(originBuffer); // you don't really have to do this, you can, but it's usualy for data you are not planing on encrypting like meta data
let cipherRet = cipher.update(originBuffer, 'this is ignored because you gave it a buffer', 'hex');
    cipherRet += cipher.final('hex'); // this is unlikely to every give you anything because of the type of cipher but it doesn't hurt to use it

var tag = cipher.getAuthTag();

decipher.setAAD(originBuffer); // see above, you don't need to do this and if you do the plain text is not what you'd want to be using as the additional authenticated data
decipher.setAuthTag(tag);
let out = decipher.update(cipherRet, 'hex'); // get it out as a buffer
decipher.final(); // this will never return anything because it's a stream cipher
xflxy commented 7 years ago

Thank you so much for the code.I really want to use ChaCha20, but I don't understand it very well. Could you put demo to encode and decode a message in the Code area? These code can't work! help!

var chacha20 = require('./browser');

var key = new Buffer('3zTvzr3p67VC61jmV54rIYu1545x4TlY'); var nonce = new Buffer('abcdef123456'); var nonencrypteddata = "1224345";

var cipher = chacha20.createCipher(key, nonce); var decipher = chacha20.createDecipher(key, nonce);

cipher.setAAD(nonencrypteddata); var tag = cipher.getAuthTag(); decipher.setAAD(nonencrypteddata); decipher.setAuthTag(tag);

image

calvinmetcalf commented 7 years ago

you need to finish encrypting all your data (by calling final) before you can call getAuthTag, then you need to call setAAD and setAuthTag before you add any data

xflxy commented 7 years ago

Thanks, The problem is solved. @calvinmetcalf

GentritMustafa commented 7 years ago

Hi @calvinmetcalf, can you give me an useful example with real data for encryption and decryption with ChaCha20 only, not ChaCha20 Poly1305? I think your instructions are incomplete. Thanks.

calvinmetcalf commented 7 years ago

@GentritMustafa there is this, do you have some questions about that?