Skip to content

Commit fd20660

Browse files
authored
Fixed browser usage
Using `const createHmac = require('create-hmac')` will ensure that this script will work both on Node.js and Browser (otherwise it will crash on browser). I've also updated `new Buffer` on line 7, which is deprecated in flavor of `Buffer.from`
1 parent e6deed2 commit fd20660

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

examples/signature.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
const crypto = require('crypto')
1+
const createHmac = require('create-hmac')
22

33
const KEY = '943b421c9eb07c830af81030552c86009268de4e532ba2ee2eab8247c6da0881'
44
const SALT = '520f986b998545b4785e0defbc4f3c1203f22de2374a3d53cb7a7fe9fea309c5'
55

66
const urlSafeBase64 = (string) => {
7-
return new Buffer(string).toString('base64').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
7+
return Buffer.from(string).toString('base64').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
88
}
99

1010
const hexDecode = (hex) => Buffer.from(hex, 'hex')
1111

1212
const sign = (salt, target, secret) => {
13-
const hmac = crypto.createHmac('sha256', hexDecode(secret))
13+
const hmac = createHmac('sha256', hexDecode(secret))
1414
hmac.update(hexDecode(salt))
1515
hmac.update(target)
1616
return urlSafeBase64(hmac.digest())

0 commit comments

Comments
 (0)