forked from EmelyanenkoK/checkproof.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
argon2.js
137 lines (114 loc) · 4.19 KB
/
argon2.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
//modified antelle.github.io/argon2-browser/
var moduleEl = null;
var listener = null;
var results = [];
var argon_enabled = !(!navigator.mimeTypes['application/x-pnacl']);
window.onload = function() {
moduleEl = document.createElement('embed');
moduleEl.setAttribute('name', 'argon2');
moduleEl.setAttribute('id', 'pnacl-argon2');
moduleEl.setAttribute('width', '0');
moduleEl.setAttribute('height', '0');
moduleEl.setAttribute('src', 'argon2.nmf');
moduleEl.setAttribute('type', 'application/x-pnacl');
listener = document.getElementById('pnaclListener');
listener.addEventListener('error', function() { console.log('Error'); }, true);
listener.addEventListener('crash', function() { console.log('Crash'); }, true);
listener.appendChild(moduleEl);
moduleEl.offsetTop; // required by PNaCl
};
//Example
var test_mass={
pass: 'password',
salt: 'somesalt',
time: 1,
mem: 1024,
hashLen: 32,
parallelism: 1,
type: 1
};
function addEventListenerOnce(target, type, listener) {
target.addEventListener(type, function fn(event) {
target.removeEventListener(type, fn);
listener(event);
},
true);
}
var argon2_pnacl = function (mass, callback) {
if (!navigator.mimeTypes['application/x-pnacl']) {
return console.log('PNaCl is not supported by your browser'); //TODO Make pop-up
}
if(callback==null) {callback = function(){}}
startTime = Date.now()
moduleEl.postMessage(mass);
listener.addEventListener('message', function(e) {
console.log("Result: ", e);
var encoded = e.data.encoded;
var hash = e.data.hash;
if (e.data.res) {
console.log('Error: ' + e.data.res + ': ' + e.data.error);
} else {
console.log('Encoded: ' + encoded);
console.log('Hash: ' + hash);
console.log('Elapsed: ' + (Date.now() - startTime) + 'ms');
callback(e.data)
}
}, true);
}
// removeEventListener doesn't work, I don't know why.
// We are using dirty hack: keep list of already worked listeners
list_used_listeners = {};
var argon2_verify_pnacl = function (mass, callback) {
if (!navigator.mimeTypes['application/x-pnacl']) {
return console.log('PNaCl is not supported by your browser'); //TODO Make pop-up
}
//listener = $('pnaclListener');
if(callback==null) {callback = function(){}}
startTime = Date.now()
moduleEl.postMessage(mass);
function response_action(unique_tag,e) {
if(list_used_listeners[unique_tag])
return;
list_used_listeners[unique_tag]=true;
console.log("Result: ", e);
var encoded = e.data.encoded;
var hash = e.data.hash;
if (!e.data.res) {
console.log('Error: ' + e.data.res + ': ' + e.data.error + ' '+ e.data.message);
}
callback(e.data);
//listener.outerHTML=listener.outerHTML;//.removeEventListener("message", response_action);
}
//listener.addEventListener("message", response_action, true);
var d = new Date();
var n = d.getTime();
addEventListenerOnce(listener, 'message', response_action.bind(null, n));
//listener.one('message', response_action);
}
function base64toHEX(base64) {
var raw = atob(base64);
var HEX = '';
for ( i = 0; i < raw.length; i++ ) {
var _hex = raw.charCodeAt(i).toString(16)
HEX += (_hex.length==2?_hex:'0'+_hex);
}
return HEX.toUpperCase();
}
function b64_to_utf8(str) {
return decodeURIComponent(escape(window.atob(str)));
}
// hash - string which we want to hash, `password` for example
// encodedString - argon2 string like $argon2i$v=19$m=1024,t=1,p=1$c29tZXNhbHQ$lUZsxPkvh0lUYX7sCqEZXSKYCr1iXlysRHY646nLarc
var argon2_verify = function(password, encodedString, callback) {
elements = encodedString.split('$');
if (elements[1]=='argon2d')
{ type=0; }
if (elements[1]=='argon2i')
{ type=1; }
argon_mass = { 'method': 'argon2_verify','type': type, 'encodedString': encodedString, 'pass': password, hashLen: 32};
var enhanced_callback = function(data)
{
callback(data.res, base64toHEX(elements[elements.length-1]));
}
argon2_verify_pnacl(argon_mass, enhanced_callback);
}