1
0

signature-truncated.mjs 779 B

123456789101112131415161718192021
  1. import { createHmac } from 'node:crypto';
  2. const KEY = '943b421c9eb07c830af81030552c86009268de4e532ba2ee2eab8247c6da0881'
  3. const SALT = '520f986b998545b4785e0defbc4f3c1203f22de2374a3d53cb7a7fe9fea309c5'
  4. const SIGNATURE_SIZE = 8 // should correspond to IMGPROXY_SIGNATURE_SIZE on the server. Default: 32
  5. const hexDecode = (hex) => Buffer.from(hex, 'hex')
  6. const sign = (salt, target, secret, size) => {
  7. const hmac = createHmac('sha256', hexDecode(secret))
  8. hmac.update(hexDecode(salt))
  9. hmac.update(target)
  10. return Buffer.from(hmac.digest().slice(0, size)).toString('base64url')
  11. }
  12. const path = "/rs:fit:300:300/plain/http://img.example.com/pretty/image.jpg"
  13. const signature = sign(SALT, path, KEY, SIGNATURE_SIZE)
  14. const result = `/${signature}${path}`
  15. console.log(result)