-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecdh.ts
More file actions
36 lines (32 loc) · 845 Bytes
/
ecdh.ts
File metadata and controls
36 lines (32 loc) · 845 Bytes
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
import { buf } from './buffer'
export async function generateKeyPair(): Promise<CryptoKeyPair> {
return crypto.subtle.generateKey(
{ name: 'ECDH', namedCurve: 'P-256' },
true,
['deriveBits']
)
}
export async function exportPublicKey(key: CryptoKey): Promise<Uint8Array> {
const raw = await crypto.subtle.exportKey('raw', key)
return new Uint8Array(raw)
}
export async function importPublicKey(raw: Uint8Array): Promise<CryptoKey> {
return crypto.subtle.importKey(
'raw',
buf(raw),
{ name: 'ECDH', namedCurve: 'P-256' },
true,
[]
)
}
export async function deriveSharedSecret(
privateKey: CryptoKey,
publicKey: CryptoKey,
): Promise<Uint8Array> {
const bits = await crypto.subtle.deriveBits(
{ name: 'ECDH', public: publicKey },
privateKey,
256
)
return new Uint8Array(bits)
}