Skip to content

Instantly share code, notes, and snippets.

@iahu
Created May 31, 2021 02:45
Show Gist options
  • Save iahu/03d78907c6b1e5425fd84356a2563677 to your computer and use it in GitHub Desktop.
Save iahu/03d78907c6b1e5425fd84356a2563677 to your computer and use it in GitHub Desktop.
DSP-ADX 价格解密逻辑 Typescript 版
import * as crypto from 'crypto'
const HMAC_ALGORITHM = 'HmacSHA1'
const ekey = Buffer.from('9527')
const ikey = Buffer.from('2840')
const mergeBytes = (...args: Uint8Array[]) => {
return Buffer.concat(args)
}
const bytesToLong = (b: Buffer) => b.readBigInt64BE()
const longToBytes = (n: bigint) => {
const b = Buffer.alloc(8)
b.writeBigInt64BE(n)
return b
}
const decrypt = (encyptStr: string) => {
const decode = Buffer.from(encyptStr, 'base64')
const ekeyMac = crypto.createHmac('sha1', ekey)
const ikeyMac = crypto.createHmac('sha1', ikey)
const length = decode.length
const impressionIdBytes = decode.subarray(0, 16)
const priceBytes = decode.subarray(16, length - 4)
const requestSign = decode.subarray(length - 4, length)
const requestIdMac = ekeyMac.update(impressionIdBytes).digest()
const requestIdMacPart = requestIdMac.subarray(0, 8)
const temp = requestIdMacPart.readBigInt64BE()
const price = priceBytes.readBigInt64BE() ^ temp
const data = ikeyMac
.update(mergeBytes(longToBytes(price), impressionIdBytes))
.digest()
const sign = data.subarray(0, 4)
for (let i = 0; i < 4; ++i) {
if (sign[i] !== requestSign[i]) {
throw new Error('参数有误,验签失败')
}
}
return price
}
// decrypt('MTIzNDU2Nzg5MTIzNDU2N-9-9pSOh7qyG_4KZg==') // 1010
export default decrypt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment