-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathPublicCipherTests.ts
More file actions
184 lines (164 loc) · 5.69 KB
/
PublicCipherTests.ts
File metadata and controls
184 lines (164 loc) · 5.69 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { assert, expect } from 'chai';
import { Buffer } from '@craftzdog/react-native-buffer';
import { describe, it } from '../../MochaRNAdapter';
import crypto from 'react-native-quick-crypto';
import type { KeyPairKey } from '../../../../../react-native-quick-crypto/src/Cipher';
import type { EncodingOptions } from '../../../../../react-native-quick-crypto/src/keys';
// import { PrivateKey } from 'sscrypto/node';
const message = 'Hello Node.js world!';
// Tests that a key pair can be used for encryption / decryption.
function testEncryptDecrypt(publicKey: KeyPairKey, privateKey: KeyPairKey) {
const plaintext = Buffer.from(message, 'utf8');
for (const key of [publicKey, privateKey]) {
// the EncodingOptions type is weird as shit, but it works.
// Someone else is welcome to wade through rsaFunctionFor and figure out a
// better way.
const ciphertext = crypto.publicEncrypt(key as EncodingOptions, plaintext);
const received = crypto.privateDecrypt(
privateKey as EncodingOptions,
ciphertext,
);
assert.strictEqual(received.toString('utf8'), message);
}
}
// I guess interally this functions use privateEncrypt/publicDecrypt (sign/verify)
// but the main function `sign` is not implemented yet
// Tests that a key pair can be used for signing / verification.
// function testSignVerify(publicKey: any, privateKey: any) {
// const message = Buffer.from('Hello Node.js world!');
// function oldSign(algo, data, key) {
// return createSign(algo).update(data).sign(key);
// }
// function oldVerify(algo, data, key, signature) {
// return createVerify(algo).update(data).verify(key, signature);
// }
// for (const signFn of [sign, oldSign]) {
// const signature = signFn('SHA256', message, privateKey);
// for (const verifyFn of [verify, oldVerify]) {
// for (const key of [publicKey, privateKey]) {
// const okay = verifyFn('SHA256', message, key, signature);
// assert(okay);
// }
// }
// }
// }
describe('publicCipher', () => {
// // We need to monkey patch sscrypto to use all the crypto functions from quick-crypto
// it('sscrypto basic test', async () => {
// try {
// const clearText = 'This is clear text';
// const privateKey = await PrivateKey.generate(1024);
// const encrypted = privateKey.encrypt(Buffer.from(clearText) as any);
// const decrypted = privateKey.decrypt(encrypted);
// expect(decrypted.toString('utf-8')).to.equal(clearText);
// } catch (e) {
// assert.fail();
// }
// });
// it('publicEncrypt/privateDecrypt', () => {
// const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
// modulusLength: 512,
// publicKeyEncoding: {
// type: 'pkcs1',
// format: 'pem',
// },
// privateKeyEncoding: {
// type: 'pkcs8',
// format: 'pem',
// },
// });
// testEncryptDecrypt(publicKey, privateKey);
// });
// it('publicEncrypt/privateDecrypt with non-common exponent', () => {
// const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
// publicExponent: 3,
// modulusLength: 512,
// publicKeyEncoding: {
// type: 'pkcs1',
// format: 'pem',
// },
// privateKeyEncoding: {
// type: 'pkcs8',
// format: 'pem',
// },
// });
// testEncryptDecrypt(publicKey, privateKey);
// });
// it('publicEncrypt/privateDecrypt with passphrase', () => {
// const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
// modulusLength: 4096,
// publicKeyEncoding: {
// type: 'spki',
// format: 'pem',
// },
// privateKeyEncoding: {
// type: 'pkcs8',
// format: 'pem',
// cipher: 'aes-256-cbc',
// passphrase: 'top secret',
// },
// });
// const message = 'Hello RN world!';
// const plaintext = Buffer.from(message, 'utf8');
// const ciphertext = crypto.publicEncrypt(
// publicKey as EncodingOptions,
// plaintext,
// );
// const decrypted = crypto.privateDecrypt(
// { key: privateKey, passphrase: 'top secret' },
// ciphertext,
// );
// expect(decrypted.toString('utf-8')).to.equal(message);
// });
// it('passphrased private key without passphrase should throw', () => {
// const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
// modulusLength: 4096,
// publicKeyEncoding: {
// type: 'spki',
// format: 'pem',
// },
// privateKeyEncoding: {
// type: 'pkcs8',
// format: 'pem',
// cipher: 'aes-256-cbc',
// passphrase: 'top secret',
// },
// });
// try {
// testEncryptDecrypt(publicKey, privateKey);
// assert.fail();
// // eslint-disable-next-line @typescript-eslint/no-unused-vars
// } catch (_e) {
// // intentionally left blank
// }
// });
it('#494 decryption error', () => {
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 512,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem',
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
},
});
const encryptedMessage = crypto.publicEncrypt(
{
key: publicKey,
padding: crypto.constants.RSA_PKCS1_PADDING,
},
Buffer.from(message, 'utf8')
);
const decryptedMessage = crypto.privateDecrypt(
{
key: privateKey,
padding: crypto.constants.RSA_PKCS1_PADDING,
},
encryptedMessage
);
console.log(encryptedMessage.toString('hex'));
expect(decryptedMessage.toString('utf8')).to.equal(message);
})
});