-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc4836a
commit 584026d
Showing
8 changed files
with
88 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { webcrypto } from "crypto"; | ||
import { importPublicKey } from "./importPublicKey"; | ||
|
||
export const calcSecretHash = async (secret: string, pubKey: string) => { | ||
const publicKey = await importPublicKey(pubKey, { | ||
name: "HMAC", | ||
hash: "SHA256", | ||
}); | ||
const hash = await webcrypto.subtle.sign( | ||
"HMAC", | ||
publicKey, | ||
new TextEncoder().encode(secret) | ||
); | ||
return new TextDecoder().decode(hash); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { webcrypto } from "crypto"; | ||
import { base64ToArrayBuffer } from "koishi"; | ||
|
||
export const importPublicKey = async ( | ||
pemKey: string, | ||
algorithm: Parameters<webcrypto.SubtleCrypto["importKey"]>[2] | ||
) => | ||
await webcrypto.subtle.importKey( | ||
"spki", | ||
base64ToArrayBuffer(pemKey.slice(26, -24).trim()), | ||
algorithm, | ||
false, | ||
["sign", "verify"] | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { webcrypto } from "crypto"; | ||
import { base64ToArrayBuffer } from "koishi"; | ||
import { importPublicKey } from "./importPublicKey"; | ||
|
||
export const verifyCallback = async ( | ||
signature: string, | ||
secret: string, | ||
pubKey: string, | ||
body?: string | ||
) => { | ||
const sign = base64ToArrayBuffer(signature); | ||
const data = new URLSearchParams({ | ||
body: body ?? "", | ||
secret, | ||
}).toString(); | ||
const hash = await webcrypto.subtle.digest( | ||
"SHA-256", | ||
new TextEncoder().encode(data) | ||
); | ||
|
||
const publicKey = await importPublicKey(pubKey, { | ||
name: "RSA-PKCS1-v1_5", | ||
hash: "SHA-256", | ||
}); | ||
return await webcrypto.subtle.verify( | ||
"RSASSA-PKCS1-v1_5", | ||
publicKey, | ||
sign, | ||
hash | ||
); | ||
}; |