-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypto.test.ts
35 lines (34 loc) · 1.14 KB
/
crypto.test.ts
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
import { test } from '@jest/globals';
import { expect } from '@jest/globals';
import { describe } from 'node:test';
import { webcrypto } from 'crypto';
import { CryptoManager } from '../src/crypto';
describe('CryptoManager', () => {
const cryptoManager = new CryptoManager(webcrypto.subtle);
test('should get keypair', callback => {
cryptoManager
.generateKeyPair()
.then(keyPair => {
callback();
expect(keyPair).not.toBeNull();
expect(keyPair.publicKey).not.toBeNull();
expect(keyPair.privateKey).not.toBeNull();
console.log('keyPair', JSON.stringify(keyPair));
})
.catch(e => callback(e));
});
test('should encrypt and decrypt well', callback => {
cryptoManager.generateKeyPair().then(keyPair => {
const data = { foo: 'bar' };
cryptoManager
.encrypt(keyPair.publicKey, JSON.stringify(data))
.then(encrypted => {
cryptoManager.decrypt(keyPair.privateKey, encrypted).then(decrypted => {
expect(decrypted).toEqual(JSON.stringify(data));
callback();
});
})
.catch(e => callback(e));
});
});
});