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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
"use strict";
const crypto = require('crypto');
const Poly1305 = require('poly1305-js');
const Util = require('./util');
const XSalsa20 = require('xsalsa20');
/* istanbul ignore if */
if (typeof (Buffer) === 'undefined') {
let Buffer = require('buffer/').Buffer;
}
module.exports = class SodiumPolyfill {
/**
* @param {string|Buffer} message
* @param {CryptographyKey} key
* @return {Promise<Buffer>}
*/
static async crypto_onetimeauth(message, key) {
return Poly1305.onetimeauth(
await Util.toBuffer(message),
key.getBuffer()
);
}
/**
* @param {string|Buffer} message
* @param {CryptographyKey} key
* @param {Buffer} tag
* @return {Promise<boolean>}
*/
static async crypto_onetimeauth_verify(message, key, tag) {
return Poly1305.onetimeauth_verify(
await Util.toBuffer(message),
key.getBuffer(),
await Util.toBuffer(tag)
);
}
/**
* @param {string|Buffer} plaintext
* @param {Buffer} nonce
* @param {CryptographyKey} key
* @return {Promise<Buffer>}
*/
static async crypto_stream_xor(plaintext, nonce, key) {
const stream = XSalsa20(nonce, key.getBuffer());
const output = stream.update(plaintext);
stream.finalize();
return Util.toBuffer(output);
}
/**
* Polyfill crypto_pwhash_str_needs_rehash() for bindings that don't
* include this (somewhat new) helper function.
*
* @param {string|Buffer} hash
* @param {number} opslimit
* @param {number} memlimit
* @return {Promise<boolean>}
*/
static async crypto_pwhash_str_needs_rehash(hash, opslimit, memlimit) {
const pwhash = (await Util.toBuffer(hash)).toString('utf-8');
const pieces = pwhash.split('$');
const expect = 'm=' + (memlimit >> 10) + ',t=' + opslimit + ',p=1';
if (expect.length !== pieces[3].length) {
return true;
}
return !crypto.timingSafeEqual(
await Util.toBuffer(expect),
await Util.toBuffer(pieces[3])
);
}
};
|