28 lines
722 B
TypeScript
28 lines
722 B
TypeScript
import crypto from 'crypto'
|
|
|
|
export const sha256 = (value: string | Buffer) => crypto.createHash('sha256').update(value).digest('hex')
|
|
|
|
export const createCanonicalSignature = (input: {
|
|
secret: string
|
|
timestamp: string
|
|
nonce: string
|
|
method: string
|
|
path: string
|
|
body: string | Buffer
|
|
}) => {
|
|
const canonical = [
|
|
input.timestamp,
|
|
input.nonce,
|
|
input.method.toUpperCase(),
|
|
input.path,
|
|
sha256(input.body),
|
|
].join('\n')
|
|
return crypto.createHmac('sha256', input.secret).update(canonical).digest('hex')
|
|
}
|
|
|
|
export const safeEqual = (left: string, right: string) => {
|
|
const a = Buffer.from(left)
|
|
const b = Buffer.from(right)
|
|
return a.length === b.length && crypto.timingSafeEqual(a, b)
|
|
}
|