forked from tonyg/js-nacl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_nacl.js
94 lines (80 loc) · 4.35 KB
/
test_nacl.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
function do_tests(nacl, output) {
output("Starting...");
if ("c3a5c3a4c3b6" !== nacl.to_hex(nacl.encode_utf8("\xe5\xe4\xf6"))) {
alert("UTF-8 encoding isn't working properly");
}
output(nacl.to_hex(nacl.crypto_hash_string("hello")));
output(nacl.to_hex(nacl.crypto_hash_sha256(nacl.encode_utf8("hello"))));
var kp = nacl.crypto_box_keypair_from_seed(nacl.encode_utf8("hello"));
output("Box PK: " + nacl.to_hex(kp.boxPk));
output("Box SK: " + nacl.to_hex(kp.boxSk));
var selfShared = nacl.crypto_box_precompute(kp.boxPk, kp.boxSk);
output("Self-shared: " + nacl.to_hex(selfShared.boxK));
output("5 random bytes: " + nacl.to_hex(nacl.random_bytes(5)));
var n = nacl.crypto_box_random_nonce();
output("Nonce: " + nacl.to_hex(n));
var c = nacl.crypto_box_precomputed(nacl.encode_utf8("box test"), n, selfShared);
output("Ciphertext: " + nacl.to_hex(c));
var m = nacl.crypto_box_open_precomputed(c, n, selfShared);
output("Plaintext: " + nacl.decode_utf8(m));
c = nacl.crypto_box(nacl.encode_utf8("box test"), n, kp.boxPk, kp.boxSk);
output("Ciphertext: " + nacl.to_hex(c));
m = nacl.crypto_box_open(c, n, kp.boxPk, kp.boxSk);
output("Plaintext: " + nacl.decode_utf8(m));
output("");
n = new Uint8Array(nacl.crypto_stream_NONCEBYTES);
k = new Uint8Array(nacl.crypto_stream_KEYBYTES);
output("Stream: " + nacl.to_hex(nacl.crypto_stream(10, n, k)));
c = nacl.crypto_stream_xor(nacl.encode_utf8("hello"), n, k);
output("Ciphertext: " + nacl.to_hex(c));
m = nacl.crypto_stream_xor(c, n, k);
output("Plaintext: " + nacl.decode_utf8(m));
var authkey = nacl.crypto_hash(nacl.encode_utf8("hello")).subarray(0, nacl.crypto_onetimeauth_KEYBYTES);
var auth = nacl.crypto_onetimeauth(nacl.encode_utf8("hello"), authkey);
output("Auth key: " + nacl.to_hex(authkey));
output("Authenticator: " + nacl.to_hex(auth));
output("True: " + nacl.crypto_onetimeauth_verify(auth, nacl.encode_utf8("hello"), authkey));
output("False: " + nacl.crypto_onetimeauth_verify(auth, nacl.encode_utf8("hellp"), authkey));
output("False: " + nacl.crypto_onetimeauth_verify(auth.subarray(1), nacl.encode_utf8("hello"), authkey));
auth[0] = auth[0] + 1;
output("False: " + nacl.crypto_onetimeauth_verify(auth, nacl.encode_utf8("hello"), authkey));
output("");
var authkey = nacl.crypto_hash(nacl.encode_utf8("hello")).subarray(0, nacl.crypto_auth_KEYBYTES);
var auth = nacl.crypto_auth(nacl.encode_utf8("hello"), authkey);
output("Auth key: " + nacl.to_hex(authkey));
output("Authenticator: " + nacl.to_hex(auth));
output("True: " + nacl.crypto_auth_verify(auth, nacl.encode_utf8("hello"), authkey));
output("False: " + nacl.crypto_auth_verify(auth, nacl.encode_utf8("hellp"), authkey));
output("False: " + nacl.crypto_auth_verify(auth.subarray(1), nacl.encode_utf8("hello"), authkey));
auth[0] = auth[0] + 1;
output("False: " + nacl.crypto_auth_verify(auth, nacl.encode_utf8("hello"), authkey));
output("");
n = nacl.crypto_secretbox_random_nonce();
var secretboxkey = nacl.crypto_hash(nacl.encode_utf8("hello")).subarray(0, nacl.crypto_secretbox_KEYBYTES);
output("Nonce: " + nacl.to_hex(n));
c = nacl.crypto_secretbox(nacl.encode_utf8("hello"), n, secretboxkey);
output("Ciphertext: " + nacl.to_hex(c));
m = nacl.crypto_secretbox_open(c, n, secretboxkey);
output("Plaintext: " + nacl.decode_utf8(m));
output("");
kp = nacl.crypto_sign_keypair_from_seed(nacl.encode_utf8("hello"));
output("Signing PK: " + nacl.to_hex(kp.signPk));
output("Signing SK: " + nacl.to_hex(kp.signSk));
c = nacl.crypto_sign(nacl.encode_utf8("message"), kp.signSk);
output("Signed message: " + nacl.to_hex(c));
output("Signed message: " + nacl.decode_latin1(c));
m = nacl.crypto_sign_open(c, kp.signPk);
output("Opened message: " + nacl.decode_utf8(m));
c = nacl.decode_latin1(c);
c = c.substring(0, 35) + '!' + c.substring(36);
c = nacl.encode_latin1(c);
output("Corrupt message: " + nacl.decode_latin1(c));
m = nacl.crypto_sign_open(c, kp.signPk);
output("Detected corruption: " + (m === null));
output("");
output("...done.");
}
// export common.js module to allow one js file for browser and node.js
if (typeof module !== 'undefined' && module.exports) {
module.exports.do_tests = do_tests;
}