Skip to content

Commit 736ef04

Browse files
author
tom zhou
committed
tweetnacl/fast hash/box/open/secret-box/open/sign/verify working
1 parent 2ce4b5b commit 736ef04

File tree

10 files changed

+7420
-89
lines changed

10 files changed

+7420
-89
lines changed

bench/bench-fast.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
var nacl = require('./nacl-fast');
2+
3+
var m0 = new Uint8Array(new Buffer('Helloword, Am Tom ...'));
4+
m0[0] = 0x68;
5+
6+
var ska = new Uint8Array(32);
7+
for (var i = 0; i < ska.length; i ++) ska[i] = 0;
8+
var kpa = nacl.box.keyPair.fromSecretKey(ska);
9+
console.log('\nkpa:'+JSON.stringify(kpa));
10+
11+
var skb = new Uint8Array(32);
12+
for (var i = 0; i < skb.length; i ++) skb[i] = 1;
13+
var kpb = nacl.box.keyPair.fromSecretKey(skb);
14+
console.log('\nkpb:'+JSON.stringify(kpb));
15+
16+
var nonce = new Uint8Array(24);
17+
for (var i = 0; i < nonce.length; i ++) nonce[i] = 0;
18+
console.log('\nnonce:'+JSON.stringify(nonce));
19+
20+
var cab = nacl.box(m0, nonce, kpb.publicKey, kpa.secretKey);
21+
console.log('\ncab:'+JSON.stringify(cab));
22+
23+
24+
console.log('\nm0:'+JSON.stringify(m0));
25+
26+
var mba = nacl.box.open(cab, nonce, kpa.publicKey, kpb.secretKey);
27+
console.log('\nmba:'+JSON.stringify(mba));
28+
29+
var nm0 = mba.toString('utf-8');
30+
console.log('\nnm0:'+nm0);
31+
32+
// Stress on secretBox
33+
34+
// shared key
35+
var i;
36+
37+
var shk = new Uint8Array(nacl.secretbox.keyLength);
38+
for (i = 0; i < shk.length; i ++)
39+
shk[i] = 0x66;
40+
41+
var nonce = new Uint8Array(nacl.secretbox.nonceLength);
42+
for (i = 0; i < nonce.length; i ++)
43+
nonce[i] = 0;
44+
45+
// messages
46+
var m0 = "Helloword, Am Tom ...";
47+
48+
// cipher A -> B
49+
console.log("streess on secret box@"+m0);
50+
51+
for (var t = 0; t < 19; t ++, m0 += m0) {
52+
var mb0 = new Uint8Array(new Buffer(m0));
53+
54+
console.log("\n\n\tstreess/"+(mb0.length/1000.0) +"kB: " + t + " times");
55+
///console.log('mb0:'+JSON.stringify(mb0));
56+
57+
console.log("secret box ...@" + new Date().getTime());
58+
var cab = nacl.secretbox(mb0, nonce, shk);
59+
console.log("... secret box@" + new Date().getTime());
60+
///console.log('cab:'+JSON.stringify(cab));
61+
62+
console.log("\nsecret box open ...@" + new Date().getTime());
63+
var mba = nacl.secretbox.open(cab, nonce, shk);
64+
console.log("... secret box open@" + new Date().getTime());
65+
///console.log('mba:'+JSON.stringify(mba));
66+
}
67+
68+
// signature
69+
var m1 = "Helloword, Am Tom ...";
70+
var seed = new Uint8Array(32); for (var i = 0; i < 32; i ++) seed[i] = 0x66;
71+
var sigk = nacl.sign.keyPair.fromSeed(seed);
72+
console.log('\nsigk:'+JSON.stringify(sigk));
73+
74+
var sig = nacl.sign(new Uint8Array(new Buffer(m1)), sigk.secretKey);
75+
console.log('\nsignature:'+JSON.stringify(sig));
76+
77+
var msg = nacl.sign.open(sig, sigk.publicKey);
78+
console.log('\nveriry:'+JSON.stringify(msg));
79+
80+
// hash
81+
var hash = nacl.hash(new Uint8Array(new Buffer(m1)));
82+
console.log('\nhash@'+m1+'/'+new Buffer(m1).length +':'+JSON.stringify(hash));

bench/bench.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ int main(int argc, char *argv[]) {
2828
nonce[i] = 0x68;
2929

3030
// messages
31-
string m0 = "Helloword, TweetNacl...";
31+
string m0 = "Helloword, Am Tom ...";
3232

3333
// cipher A -> B
3434
cout << "streess on secret box@"+m0;
3535

3636
timeval curTime;
3737

38-
for (int t = 0; t < 19; t ++, m0 += m0) {
38+
for (int t = 0; t < 18; t ++, m0 += m0) {
3939
const char * mb0 = m0.c_str();
4040

4141
printf("\n\n\tstreess/%fkB: %d times\n", m0.length()/1000.0, t);
@@ -59,6 +59,21 @@ int main(int argc, char *argv[]) {
5959
gettimeofday(&curTime, NULL);
6060
printf("... secret box open@%6.3f\n", curTime.tv_sec*1000.0 + (curTime.tv_usec / 1000.0));
6161
}
62+
63+
// signature
64+
///string m2 = "Helloword, TweetNaclFast...";
65+
///unsigned char sign[64+m2.length()];
66+
67+
68+
69+
// hash
70+
string m1 = "Helloword, TweetNaclFast...";
71+
unsigned char hash[64];
72+
crypto_hash(hash,(const unsigned char*)m1.c_str(),m1.length());
73+
74+
printf("\nhash:");
75+
for (int i = 0; i < 64; i ++) printf(" %d", hash[i]);
76+
printf("\n");
6277

6378
return 0;
6479
}

bench/bench.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
// TweetNacl.js test bench
2-
3-
41
var nacl = require('./nacl');
52

6-
var m0 = new Uint8Array(1);///('Helloword, TweetNacl...', 'utf-8');
3+
var m0 = new Uint8Array(new Buffer('Helloword, Am Tom ...'));
74
m0[0] = 0x68;
85

96
var ska = new Uint8Array(32);
@@ -43,10 +40,10 @@ for (i = 0; i < shk.length; i ++)
4340

4441
var nonce = new Uint8Array(nacl.secretbox.nonceLength);
4542
for (i = 0; i < nonce.length; i ++)
46-
nonce[i] = 0x68;
43+
nonce[i] = 0;
4744

4845
// messages
49-
var m0 = "Helloword, TweetNacl...";
46+
var m0 = "Helloword, Am Tom ...";
5047

5148
// cipher A -> B
5249
console.log("streess on secret box@"+m0);
@@ -55,13 +52,31 @@ for (var t = 0; t < 19; t ++, m0 += m0) {
5552
var mb0 = new Uint8Array(new Buffer(m0));
5653

5754
console.log("\n\n\tstreess/"+(mb0.length/1000.0) +"kB: " + t + " times");
55+
///console.log('mb0:'+JSON.stringify(mb0));
5856

5957
console.log("secret box ...@" + new Date().getTime());
6058
var cab = nacl.secretbox(mb0, nonce, shk);
6159
console.log("... secret box@" + new Date().getTime());
60+
///console.log('cab:'+JSON.stringify(cab));
6261

6362
console.log("\nsecret box open ...@" + new Date().getTime());
6463
var mba = nacl.secretbox.open(cab, nonce, shk);
6564
console.log("... secret box open@" + new Date().getTime());
65+
///console.log('mba:'+JSON.stringify(mba));
6666
}
6767

68+
// signature
69+
var m1 = "Helloword, Am Tom ...";
70+
var seed = new Uint8Array(32); for (var i = 0; i < 32; i ++) seed[i] = 0x66;
71+
var sigk = nacl.sign.keyPair.fromSeed(seed);
72+
console.log('\nsigk:'+JSON.stringify(sigk));
73+
74+
var sig = nacl.sign(new Uint8Array(new Buffer(m1)), sigk.secretKey);
75+
console.log('\nsignature:'+JSON.stringify(sig));
76+
77+
var msg = nacl.sign.open(sig, sigk.publicKey);
78+
console.log('\nveriry:'+JSON.stringify(msg));
79+
80+
// hash
81+
var hash = nacl.hash(new Uint8Array(new Buffer(m1)));
82+
console.log('\nhash@'+m1+'/'+new Buffer(m1).length +':'+JSON.stringify(hash));

0 commit comments

Comments
 (0)