1
|
var crypto = require('../browser')
|
2
|
var test = require('tape')
|
3
|
var fs = require('fs')
|
4
|
var Buffer = require('safe-buffer').Buffer
|
5
|
var path = require('path')
|
6
|
// Test RSA encryption/decryption
|
7
|
test('node tests', function (t) {
|
8
|
var keyPem = fs.readFileSync(path.join(__dirname, 'test_key.pem'), 'ascii')
|
9
|
var rsaPubPem = fs.readFileSync(path.join(__dirname, 'test_rsa_pubkey.pem'),
|
10
|
'ascii')
|
11
|
var rsaKeyPem = fs.readFileSync(path.join(__dirname, 'test_rsa_privkey.pem'),
|
12
|
'ascii')
|
13
|
var rsaKeyPemEncrypted = fs.readFileSync(path.join(
|
14
|
__dirname, 'test_rsa_privkey_encrypted.pem'), 'ascii')
|
15
|
var input = 'I AM THE WALRUS'
|
16
|
var bufferToEncrypt = Buffer.from(input)
|
17
|
|
18
|
var encryptedBuffer = crypto.publicEncrypt(rsaPubPem, bufferToEncrypt)
|
19
|
|
20
|
var decryptedBuffer = crypto.privateDecrypt(rsaKeyPem, encryptedBuffer)
|
21
|
t.equal(input, decryptedBuffer.toString())
|
22
|
|
23
|
var decryptedBufferWithPassword = crypto.privateDecrypt({
|
24
|
key: rsaKeyPemEncrypted,
|
25
|
passphrase: 'password'
|
26
|
}, encryptedBuffer)
|
27
|
t.equal(input, decryptedBufferWithPassword.toString())
|
28
|
|
29
|
// encryptedBuffer = crypto.publicEncrypt(certPem, bufferToEncrypt);
|
30
|
|
31
|
// decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer);
|
32
|
// t.equal(input, decryptedBuffer.toString());
|
33
|
|
34
|
encryptedBuffer = crypto.publicEncrypt(keyPem, bufferToEncrypt)
|
35
|
|
36
|
decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer)
|
37
|
t.equal(input, decryptedBuffer.toString())
|
38
|
|
39
|
encryptedBuffer = crypto.privateEncrypt(keyPem, bufferToEncrypt)
|
40
|
|
41
|
decryptedBuffer = crypto.publicDecrypt(keyPem, encryptedBuffer)
|
42
|
t.equal(input, decryptedBuffer.toString())
|
43
|
|
44
|
t.throws(function () {
|
45
|
crypto.privateDecrypt({
|
46
|
key: rsaKeyPemEncrypted,
|
47
|
passphrase: 'wrong'
|
48
|
}, encryptedBuffer)
|
49
|
})
|
50
|
t.end()
|
51
|
})
|