-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathscrypt_cooked.js
144 lines (121 loc) · 4 KB
/
scrypt_cooked.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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var scrypt = (function () {
var exports = {};
//---------------------------------------------------------------------------
// Horrifying UTF-8 and hex codecs
function encode_utf8(s) {
return encode_latin1(unescape(encodeURIComponent(s)));
}
function encode_latin1(s) {
var result = new Uint8Array(s.length);
for (var i = 0; i < s.length; i++) {
var c = s.charCodeAt(i);
if ((c & 0xff) !== c) throw {message: "Cannot encode string in Latin1", str: s};
result[i] = (c & 0xff);
}
return result;
}
function decode_utf8(bs) {
return decodeURIComponent(escape(decode_latin1(bs)));
}
function decode_latin1(bs) {
var encoded = [];
for (var i = 0; i < bs.length; i++) {
encoded.push(String.fromCharCode(bs[i]));
}
return encoded.join('');
}
function to_hex(bs) {
var encoded = [];
for (var i = 0; i < bs.length; i++) {
encoded.push("0123456789abcdef"[(bs[i] >> 4) & 15]);
encoded.push("0123456789abcdef"[bs[i] & 15]);
}
return encoded.join('');
}
//---------------------------------------------------------------------------
function injectBytes(bs, leftPadding) {
var p = leftPadding || 0;
var address = scrypt_raw._malloc(bs.length + p);
scrypt_raw.HEAPU8.set(bs, address + p);
for (var i = address; i < address + p; i++) {
scrypt_raw.HEAPU8[i] = 0;
}
return address;
}
function check_injectBytes(function_name, what, thing, expected_length, leftPadding) {
check_length(function_name, what, thing, expected_length);
return injectBytes(thing, leftPadding);
}
function extractBytes(address, length) {
var result = new Uint8Array(length);
result.set(scrypt_raw.HEAPU8.subarray(address, address + length));
return result;
}
//---------------------------------------------------------------------------
function check(function_name, result) {
if (result !== 0) {
throw {message: "scrypt_raw." + function_name + " signalled an error"};
}
}
function check_length(function_name, what, thing, expected_length) {
if (thing.length !== expected_length) {
throw {message: "scrypt." + function_name + " expected " +
expected_length + "-byte " + what + " but got length " + thing.length};
}
}
function Target(length) {
this.length = length;
this.address = scrypt_raw._malloc(length);
}
Target.prototype.extractBytes = function (offset) {
var result = extractBytes(this.address + (offset || 0), this.length - (offset || 0));
scrypt_raw._free(this.address);
this.address = null;
return result;
};
function free_all(addresses) {
for (var i = 0; i < addresses.length; i++) {
scrypt_raw._free(addresses[i]);
}
}
//---------------------------------------------------------------------------
function random_bytes(count) {
var bs = new Uint8Array(count);
if(typeof(window.crypto) !== "undefined") {
if(typeof(window.crypto.getRandomValues) !== "undefined") {
window.crypto.getRandomValues(bs);
return bs;
}
}
if(typeof(window.msCrypto) !== "undefined") {
if(typeof(window.msCrypto.getRandomValues) !== "undefined") {
window.msCrypto.getRandomValues(bs);
return bs;
}
}
throw { message: "No suitable random number generator found!"};
}
function crypto_scrypt(passwd, salt, n, r, p, buflen) {
var buf = new Target(buflen);
var pa = injectBytes(passwd);
var sa = injectBytes(salt);
check("_crypto_scrypt",
scrypt_raw._crypto_scrypt(pa, passwd.length,
sa, salt.length,
n, 0, // 64 bits; zero upper half
r,
p,
buf.address, buf.length));
free_all([pa, sa]);
return buf.extractBytes();
}
//---------------------------------------------------------------------------
exports.encode_utf8 = encode_utf8;
exports.encode_latin1 = encode_latin1;
exports.decode_utf8 = decode_utf8;
exports.decode_latin1 = decode_latin1;
exports.to_hex = to_hex;
exports.random_bytes = random_bytes;
exports.crypto_scrypt = crypto_scrypt;
return exports;
})();