-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa.ts
More file actions
100 lines (89 loc) · 2.75 KB
/
Copy pathrsa.ts
File metadata and controls
100 lines (89 loc) · 2.75 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
import forge from 'node-forge'
import cbor from 'cbor'
type encodingType = "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" | undefined
export const generateKeyPair = (
options: forge.pki.rsa.GenerateKeyPairOptions = { bits: 2048, workers: 2, e: 0x10001 }
): Promise<{
publicKey: string
privateKey: string
}> => {
return new Promise((resolve) => {
forge.pki.rsa.generateKeyPair(options, (err, keypair) => {
resolve({
publicKey: forge.pki.publicKeyToPem(keypair.publicKey),
privateKey: forge.pki.privateKeyToPem(keypair.privateKey),
})
})
})
}
export const encrypt = (
message: string,
publicKey: string,
format: encodingType = 'base64'
) => {
return Buffer.from(forge.pki.publicKeyFromPem(publicKey).encrypt(forge.util.encodeUtf8(message))).toString(format)
}
export const decrypt = (
encrypted: string,
privateKey: string,
formmat: encodingType = 'base64'
) => {
return forge.pki.privateKeyFromPem(privateKey).decrypt(Buffer.from(encrypted, formmat).toString())
}
export const _sign = (
message: string,
privateKey: string,
format: encodingType = 'base64'
) => {
const md = forge.md.sha1.create()
md.update(forge.util.encodeUtf8(message), 'utf8')
return Buffer.from(forge.pki.privateKeyFromPem(privateKey).sign(md)).toString(format)
}
export const _verify = (
signature: string,
message: string,
publicKey: string,
format: encodingType = 'base64'
) => {
const md = forge.md.sha1.create()
md.update(forge.util.encodeUtf8(message), 'utf8')
return forge.pki.publicKeyFromPem(publicKey).verify(md.digest().bytes(), Buffer.from(signature, format).toString())
}
export const sign = (
message: string,
privateKey: string,
format: encodingType = 'base64'
) => {
const md = forge.md.sha1.create()
md.update(forge.util.encodeUtf8(message), 'utf8')
const signedObject = {
sign: _sign(message, privateKey, format),
message,
}
return cbor.encode(signedObject).toString(format)
}
export const extract = (
signature: string,
format: encodingType = 'base64'
): undefined | {
sign: string
message: string
} => {
const signedObject = cbor.decode(
Buffer.from(signature, format)
)
if (typeof signedObject.sign != 'string') return undefined
if (typeof signedObject.message != 'string') return undefined
return signedObject
}
export const verify = (
signature: string,
message: string,
publicKey: string,
format: encodingType = 'base64'
) => {
const signedObject = cbor.decode(
Buffer.from(signature, format)
)
return _verify(signedObject.sign, message, publicKey, format)
}