Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const moment = require('moment')
- const base64 = require('rfc4648').base64
- const NodeRSA = require('node-rsa')
- const { crc32 } = require('crc')
- const rjust = require('string-just').rjust
- const chunk = require('lodash.chunk')
- const forEach = require('lodash.foreach')
- const times = require('lodash.times')
- //const Buffer = require('Buffer')
- function sign_request(request, adp_token, private_key) {
- path = request.baseUrl + request.url
- query = request.qs
- url = path
- if (query) url += `?${query}`
- request.headers = request.headers || {}
- each(_sign_request(url, request.method, request.body, adp_token, private_key), function(key, value) {
- request.headers[key] = value
- })
- return request
- }
- function _sign_request(url, method, body, adp_token, private_key, date) {
- date = date || moment().utc().format('YYYY-MM-DDTHH:mm:ssZ')
- data = `${method}\n${url}\n${date}\n`
- if (body) data += body
- data += "\n"
- data += adp_token
- // digest = OpenSSL::Digest.new("sha256")
- signed = private_key.sign(data, 'base64')
- signature = `${signed}:${date}`
- return {
- "x-adp-token": adp_token,
- "x-adp-alg": "SHA256withRSA:1.0",
- "x-adp-signature": signature
- }
- }
- function _toHex(int) {
- //return Buffer.from(str, 'utf8').toString('hex');
- return int.toString(16)
- }
- function _ToInteger(x) {
- x = Number(x)
- return x < 0 ? Math.ceil(x) : Math.floor(x)
- }
- function _modulo(a, b) {
- return a - Math.floor(a/b)*b
- }
- function _ToUint32(x) {
- return _modulo(_ToInteger(x), Math.pow(2, 32))
- }
- function _ToUint16(x) {
- return _modulo(_ToInteger(x), Math.pow(2, 8))
- }
- function _ToUint8(x) {
- return _modulo(_ToInteger(x), Math.pow(2, 8))
- }
- function _ord(char) {
- return char ? char.charCodeAt(0) : (0 & 255)
- }
- function encrypt_metadata(metadata) {
- const crc = _toHex(crc32(JSON.stringify(metadata,null,0).toString(16)))
- const checksum = rjust(crc, 8, '0').toUpperCase() // Ensure it is always 8 chars
- const object = `${checksum}#${JSON.stringify(metadata)}`
- const rounds = Math.ceil(object.length / 4);
- let temp2 = []
- for (let i = 0; i < rounds; i++) {
- temp2[i] = (object.charCodeAt(i * 4) & 255) +
- ((object.charCodeAt(i * 4 + 1) & 255) << 8) +
- ((object.charCodeAt(i * 4 + 2) & 255) << 16) +
- ((object.charCodeAt(i * 4 + 3) & 255) << 24);
- }
- const wrap = 2654435769;
- const temp3 = [1888420705, 2576816180, 2347232058, 874813317];
- let minor_rounds = Math.floor(6 + (52 / rounds));
- let first = temp2[0];
- let last = temp2[rounds - 1];
- let inner_roll = 0;
- while (minor_rounds-- > 0) {
- inner_roll += wrap;
- const inner_variable = inner_roll >>> 2 & 3;
- for (var i = 0; i < rounds; i++) {
- first = temp2[(i + 1) % rounds];
- last = temp2[i] +=
- (last >>> 5 ^ first << 2) + (first >>> 3 ^ last << 4) ^
- (inner_roll ^ first) + (temp3[i & 3 ^ inner_variable] ^ last);
- }
- }
- let final_round = [];
- for (let i = 0; i < rounds; i++) {
- final_round[i] = String.fromCharCode(
- temp2[i] & 255,
- temp2[i] >>> 8 & 255,
- temp2[i] >>> 16 & 255,
- temp2[i] >>> 24 & 255
- );
- }
- final_round = final_round.join("")
- const base64_encoded = base64encode(final_round);
- const final = 'ECdITeCs:' + base64_encoded;
- return final
- }
- function base64decode(data) {
- const buf = Buffer.from(base64.parse(data)).toString('utf8')
- return buf
- }
- function base64encode(data) {
- let out = []
- for (let i = 0; i < data.length; ++i) {
- out[i] = data.charCodeAt(i)
- }
- return base64.stringify(out)
- }
- function decrypt_metadata(encrypted_metadata) {
- // Rmove the key
- encrypted_metadata = encrypted_metadata.replace(/ECdITeCs:/, "")
- const final_round = base64decode(encrypted_metadata)
- const chunks = chunk(final_round, 4)
- let temp2 = []
- for (let i = 0; i < chunks.length; i++) {
- let chars = chunks[i]
- temp2[i] = (chars[0]) +
- (chars[1] << 8) +
- (chars[2] << 16) +
- (chars[3] << 24)
- }
- const rounds = temp2.length
- let minor_rounds = Math.floor(6 + (52 / rounds))
- const wrap_constant = 2654435769
- const constants = [1888420705, 2576816180, 2347232058, 874813317]
- let inner_roll = 0
- let inner_variable = 0
- times((minor_rounds + 1), function() {
- inner_roll += wrap_constant
- inner_variable = inner_roll >> 2 & 3
- })
- while (minor_rounds > 0) {
- minor_rounds -= 1
- inner_roll -= wrap_constant
- inner_variable = inner_roll >> 2 & 3
- for (let i = 0; i < rounds; i++) {
- i = rounds - i - 1
- first = temp2[(i + 1) % rounds]
- last = temp2[(i - 1) % rounds]
- last = temp2[i] -=
- ((last >> 5 ^ first << 2) + (first >> 3 ^ last << 4) ^
- (inner_roll ^ first) + (constants[i & 3 ^ inner_variable] ^ last))
- }
- }
- let object = ""
- for (let i = 0; i < temp2.length; i++) {
- let block = temp2[i]
- forEach([0, 8, 16, 24], function(align) {
- if ((block >> align & 255) > 0) {
- const charCode = (block >> align & 255)
- object += String.fromCharCode(charCode)
- }
- })
- }
- console.log(object)
- return object
- }
- module.exports = {
- sign_request,
- encrypt_metadata,
- decrypt_metadata,
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement