signature.js 618 B

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