1
|
var test = require('tape')
|
2
|
var fs = require('fs')
|
3
|
var parseKeys = require('parse-asn1')
|
4
|
var Buffer = require('safe-buffer').Buffer
|
5
|
var path = require('path')
|
6
|
|
7
|
require('./nodeTests')
|
8
|
var rsa1024 = {
|
9
|
private: fs.readFileSync(path.join(__dirname, 'rsa.1024.priv')),
|
10
|
public: fs.readFileSync(path.join(__dirname, 'rsa.1024.pub'))
|
11
|
}
|
12
|
var rsa1024priv = {
|
13
|
private: fs.readFileSync(path.join(__dirname, 'rsa.1024.priv')),
|
14
|
public: fs.readFileSync(path.join(__dirname, 'rsa.1024.priv'))
|
15
|
}
|
16
|
|
17
|
var rsa2028 = {
|
18
|
private: fs.readFileSync(path.join(__dirname, 'rsa.2028.priv')),
|
19
|
public: fs.readFileSync(path.join(__dirname, 'rsa.2028.pub'))
|
20
|
}
|
21
|
var nonrsa1024 = {
|
22
|
private: fs.readFileSync(path.join(__dirname, '1024.priv')),
|
23
|
public: fs.readFileSync(path.join(__dirname, '1024.pub'))
|
24
|
}
|
25
|
var nonrsa1024str = {
|
26
|
private: fs.readFileSync(path.join(__dirname, '1024.priv')).toString(),
|
27
|
public: fs.readFileSync(path.join(__dirname, '1024.pub')).toString()
|
28
|
}
|
29
|
var pass1024 = {
|
30
|
private: {
|
31
|
passphrase: 'fooo',
|
32
|
key: fs.readFileSync(path.join(__dirname, 'pass.1024.priv'))
|
33
|
},
|
34
|
public: fs.readFileSync(path.join(__dirname, 'pass.1024.pub'))
|
35
|
}
|
36
|
var pass2028 = {
|
37
|
private: {
|
38
|
passphrase: 'password',
|
39
|
key: fs.readFileSync(path.join(__dirname, 'rsa.pass.priv'))
|
40
|
},
|
41
|
public: fs.readFileSync(path.join(__dirname, 'rsa.pass.pub'))
|
42
|
}
|
43
|
|
44
|
var nodeCrypto = require('../')
|
45
|
var myCrypto = require('../browser')
|
46
|
function _testIt (keys, message, t) {
|
47
|
var pub = keys.public
|
48
|
var priv = keys.private
|
49
|
t.test(message.toString(), function (t) {
|
50
|
t.plan(8)
|
51
|
|
52
|
var myEnc = myCrypto.publicEncrypt(pub, message)
|
53
|
var nodeEnc = nodeCrypto.publicEncrypt(pub, message)
|
54
|
t.equals(myCrypto.privateDecrypt(priv, myEnc).toString('hex'), message.toString('hex'), 'my decrypter my message')
|
55
|
t.equals(myCrypto.privateDecrypt(priv, nodeEnc).toString('hex'), message.toString('hex'), 'my decrypter node\'s message')
|
56
|
t.equals(nodeCrypto.privateDecrypt(priv, myEnc).toString('hex'), message.toString('hex'), 'node decrypter my message')
|
57
|
t.equals(nodeCrypto.privateDecrypt(priv, nodeEnc).toString('hex'), message.toString('hex'), 'node decrypter node\'s message')
|
58
|
myEnc = myCrypto.privateEncrypt(priv, message)
|
59
|
nodeEnc = nodeCrypto.privateEncrypt(priv, message)
|
60
|
t.equals(myCrypto.publicDecrypt(pub, myEnc).toString('hex'), message.toString('hex'), 'reverse methods my decrypter my message')
|
61
|
t.equals(myCrypto.publicDecrypt(pub, nodeEnc).toString('hex'), message.toString('hex'), 'reverse methods my decrypter node\'s message')
|
62
|
t.equals(nodeCrypto.publicDecrypt(pub, myEnc).toString('hex'), message.toString('hex'), 'reverse methods node decrypter my message')
|
63
|
t.equals(nodeCrypto.publicDecrypt(pub, nodeEnc).toString('hex'), message.toString('hex'), 'reverse methods node decrypter node\'s message')
|
64
|
})
|
65
|
}
|
66
|
function testIt (keys, message, t) {
|
67
|
_testIt(keys, message, t)
|
68
|
_testIt(paddingObject(keys, 1), Buffer.concat([message, Buffer.from(' with RSA_PKCS1_PADDING')]), t)
|
69
|
var parsedKey = parseKeys(keys.public)
|
70
|
var k = parsedKey.modulus.byteLength()
|
71
|
var zBuf = Buffer.alloc(k)
|
72
|
var msg = Buffer.concat([zBuf, message, Buffer.from(' with no padding')]).slice(-k)
|
73
|
_testIt(paddingObject(keys, 3), msg, t)
|
74
|
}
|
75
|
function paddingObject (keys, padding) {
|
76
|
return {
|
77
|
public: addPadding(keys.public, padding),
|
78
|
private: addPadding(keys.private, padding)
|
79
|
}
|
80
|
}
|
81
|
function addPadding (key, padding) {
|
82
|
if (typeof key === 'string' || Buffer.isBuffer(key)) {
|
83
|
return {
|
84
|
key: key,
|
85
|
padding: padding
|
86
|
}
|
87
|
}
|
88
|
var out = {
|
89
|
key: key.key,
|
90
|
padding: padding
|
91
|
}
|
92
|
if ('passphrase' in key) {
|
93
|
out.passphrase = key.passphrase
|
94
|
}
|
95
|
return out
|
96
|
}
|
97
|
function testRun (i) {
|
98
|
test('run ' + i, function (t) {
|
99
|
testIt(rsa1024priv, Buffer.from('1024 2 private keys'), t)
|
100
|
testIt(rsa1024, Buffer.from('1024 keys'), t)
|
101
|
testIt(rsa2028, Buffer.from('2028 keys'), t)
|
102
|
testIt(nonrsa1024, Buffer.from('1024 keys non-rsa key'), t)
|
103
|
testIt(pass1024, Buffer.from('1024 keys and password'), t)
|
104
|
testIt(nonrsa1024str, Buffer.from('1024 keys non-rsa key as a string'), t)
|
105
|
testIt(pass2028, Buffer.from('2028 rsa key with variant passwords'), t)
|
106
|
})
|
107
|
}
|
108
|
|
109
|
var i = 0
|
110
|
var num = 20
|
111
|
while (++i <= num) {
|
112
|
testRun(i)
|
113
|
}
|