-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcrypto-test.js
More file actions
63 lines (44 loc) · 1.78 KB
/
crypto-test.js
File metadata and controls
63 lines (44 loc) · 1.78 KB
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
'use strict' // eslint-disable-line strict
const assert = require('assert')
const ChainsqlAPI = require('../src/index');
const c = new ChainsqlAPI();
function hex2a(hexx) {
var hex = hexx.toString();//force conversion
var str = '';
for (var i = 0; (i < hex.length && hex.substr(i, 2) !== '00'); i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
describe('crypto', () => {
it('eciesEncrypt', async function(){
var wallet = c.generateAddress()
var msg = "hello world"
var cipher = c.eciesEncrypt(msg,wallet.publicKey)
var plainTxt = c.eciesDecrypt(cipher,wallet.secret);
assert.equal(msg,plainTxt.toString())
})
it('eciesEncrypt', async function(){
var wallet = c.generateAddress()
var msg = "hello world"
var cipher = c.eciesEncrypt(msg,wallet.publicKey)
var plainTxt = c.eciesDecrypt(cipher,wallet.secret)
assert.equal(msg,plainTxt.toString())
})
it('symEncrypt && symDecrypt', async function(){
var msg = "hello world"
var symKey = "1111111111111111";
var cipher = c.symEncrypt(symKey,msg)
var plainText = c.symDecrypt(symKey,cipher)
assert.equal(msg,plainText)
var cipherGM = c.symEncrypt(symKey,msg,"softGMAlg")
var plainTextGM = c.symDecrypt(symKey,cipherGM,"softGMAlg")
assert.equal(msg,hex2a(plainTextGM))
})
it('asymEncrypt && asymDecrypt', async function(){
var accountInfo = c.generateAddress({algorithm:"softGMAlg"})
var msg = "hello world"
var cipher = c.asymEncrypt(msg,accountInfo.publicKey)
var plainText = c.asymDecrypt(cipher,accountInfo.secret)
assert.equal(msg,hex2a(plainText))
})
})