-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncrypt.mjs
51 lines (44 loc) · 1 KB
/
Encrypt.mjs
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
import {
EncryptionAlgorithms,
Headers,
Encrypt,
Recipient,
} from "#dist";
import crypto from "node:crypto";
const alg = EncryptionAlgorithms.A128GCM;
(async () => {
const secret = crypto.randomBytes(16);
//Creating a Cose Encrypt:
const cose = await Encrypt.encrypt(
[
[Headers.Algorithm, alg]
],
[
[Headers.ContentType, 0]
],
Buffer.from('hello world', 'utf8'),
secret,
new Uint8Array(),
[
Recipient.create(
[],
[
[Headers.Algorithm, EncryptionAlgorithms.Direct],
[Headers.KeyID, Buffer.from('our-secret', 'utf8')]
]
)
]
).then(s => s.encode());
console.log('COSE Encrypt created:');
console.log(cose.toString('hex'));
const decode = Encrypt.decode(cose);
const content = await decode.decrypt(
secret,
{
//limit supported algs
algorithms: [alg]
}
);
console.log('COSE Encrypt verified succesfully!')
console.log(`content: ${content.toString('utf8')}`)
})();