feat(api): 开发平台、门店相关代码
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import {
|
||||
createCipheriv,
|
||||
createHash,
|
||||
createHmac,
|
||||
randomBytes,
|
||||
} from 'node:crypto';
|
||||
|
||||
export type EncryptedStoreSecret = {
|
||||
ciphertext: Buffer;
|
||||
nonce: Buffer;
|
||||
fingerprint: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* 密钥只在此边界短暂以明文存在。数据库保存 AES-GCM 密文和随机 nonce,
|
||||
* fingerprint 是不可逆 HMAC,仅用于安全地识别同一份密钥。
|
||||
*/
|
||||
export function encryptStoreSecret(
|
||||
plaintext: string,
|
||||
keyMaterial: string,
|
||||
): EncryptedStoreSecret {
|
||||
const key = createHash('sha256').update(keyMaterial).digest();
|
||||
const nonce = randomBytes(12);
|
||||
const cipher = createCipheriv('aes-256-gcm', key, nonce);
|
||||
const ciphertext = Buffer.concat([
|
||||
cipher.update(plaintext, 'utf8'),
|
||||
cipher.final(),
|
||||
cipher.getAuthTag(),
|
||||
]);
|
||||
return {
|
||||
ciphertext,
|
||||
nonce,
|
||||
fingerprint: createHmac('sha256', key)
|
||||
.update(plaintext)
|
||||
.digest('base64url'),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user