-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSign.mjs
50 lines (42 loc) · 1.09 KB
/
Sign.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
import { generateKeyPair } from "jose";
import {
Algorithms,
ProtectedHeaders,
UnprotectedHeaders,
Headers,
Sign,
} from "#dist";
const alg = 'ES256';
(async () => {
const signer1 = await generateKeyPair(alg);
const signer2 = await generateKeyPair(alg);
//Creating a Cose_Sign1:
const cose = await Sign.sign(
new ProtectedHeaders([]),
new UnprotectedHeaders([
[Headers.ContentType, 0]
]),
Buffer.from('hello world', 'utf8'),
[
{
key: signer1.privateKey,
protectedHeaders: new ProtectedHeaders([
[Headers.Algorithm, Algorithms[alg]]
])
},
{
key: signer2.privateKey,
protectedHeaders: new ProtectedHeaders([
[Headers.Algorithm, Algorithms[alg]]
])
}
]
).then(s => s.encode());
// Cose is a buffer
console.log('COSE_Sign created:');
console.log(cose.toString('hex'));
const decode = Sign.decode(cose);
await decode.verify(signer1.publicKey);
await decode.verify(signer2.publicKey);
console.log('COSE_Sign verified succesfully for the two recipients!')
})();