forked from calvinmetcalf/crypto-pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchacha.js
25 lines (24 loc) · 770 Bytes
/
chacha.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
'use strict';
var chacha = require('chacha');
var PouchPromise = require('pouchdb-promise');
exports.encrypt = function encryptChacha(data, key, nonce, aad) {
return new PouchPromise(function (yes) {
var outDoc = {};
var cipher = chacha.createCipher(key, nonce);
cipher.setAAD(aad);
outDoc.data = cipher.update(data).toString('hex');
cipher.final();
outDoc.tag = cipher.getAuthTag().toString('hex');
yes(outDoc);
});
};
exports.decrypt = function decryptChacha(data, key, nonce, aad, tag) {
return new PouchPromise(function (yes) {
var decipher = chacha.createDecipher(key, nonce);
decipher.setAAD(aad);
decipher.setAuthTag(tag);
var out = decipher.update(data).toString();
decipher.final();
yes(out);
});
};