feat(store): 添加门店、门店验证
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
createDecipheriv,
|
||||
createCipheriv,
|
||||
createHash,
|
||||
createHmac,
|
||||
@@ -35,3 +36,23 @@ export function encryptStoreSecret(
|
||||
.digest('base64url'),
|
||||
};
|
||||
}
|
||||
|
||||
/** 仅供受信任的后台 Worker 使用;调用方不得记录返回值。 */
|
||||
export function decryptStoreSecret(
|
||||
ciphertextWithTag: Buffer,
|
||||
nonce: Buffer,
|
||||
keyMaterial: string,
|
||||
): string {
|
||||
const authenticationTagLength = 16;
|
||||
if (ciphertextWithTag.length <= authenticationTagLength) {
|
||||
throw new Error('Invalid encrypted store secret');
|
||||
}
|
||||
const key = createHash('sha256').update(keyMaterial).digest();
|
||||
const ciphertext = ciphertextWithTag.subarray(0, -authenticationTagLength);
|
||||
const tag = ciphertextWithTag.subarray(-authenticationTagLength);
|
||||
const decipher = createDecipheriv('aes-256-gcm', key, nonce);
|
||||
decipher.setAuthTag(tag);
|
||||
return Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString(
|
||||
'utf8',
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user