|
| 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)); |
0 commit comments