From e4bf5e93fb43be591108f31fc7d067458708f58e Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Wed, 12 Jun 2024 17:16:28 +0200 Subject: [PATCH] add javascript example for truncated signature (#163) --- examples/signature-truncated.mjs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 examples/signature-truncated.mjs diff --git a/examples/signature-truncated.mjs b/examples/signature-truncated.mjs new file mode 100644 index 00000000..472f5c6e --- /dev/null +++ b/examples/signature-truncated.mjs @@ -0,0 +1,21 @@ +import { createHmac } from 'node:crypto'; + +const KEY = '943b421c9eb07c830af81030552c86009268de4e532ba2ee2eab8247c6da0881' +const SALT = '520f986b998545b4785e0defbc4f3c1203f22de2374a3d53cb7a7fe9fea309c5' +const SIGNATURE_SIZE = 8 // should correspond to IMGPROXY_SIGNATURE_SIZE on the server. Default: 32 + +const hexDecode = (hex) => Buffer.from(hex, 'hex') + +const sign = (salt, target, secret, size) => { + const hmac = createHmac('sha256', hexDecode(secret)) + hmac.update(hexDecode(salt)) + hmac.update(target) + + return Buffer.from(hmac.digest().slice(0, size)).toString('base64url') +} + +const path = "/rs:fit:300:300/plain/http://img.example.com/pretty/image.jpg" + +const signature = sign(SALT, path, KEY, SIGNATURE_SIZE) +const result = `/${signature}${path}` +console.log(result)