import * as CryptoJS from 'crypto-js'; declare const escape: any; declare const unescape: any; declare const BigInteger: any; /** START OF THE LICENSED CODE */ /****************************************************************************** * Copyright © 2013-2016 The Nxt Core Developers. * * * * See the AUTHORS.txt, DEVELOPER-AGREEMENT.txt and LICENSE.txt files at * * the top-level directory of this distribution for the individual copyright * * holder information and the developer policies on copyright and licensing. * * * * Unless otherwise agreed in a custom licensing agreement, no part of the * * Nxt software, including this file, may be copied, modified, propagated, * * or distributed except according to the terms contained in the LICENSE.txt * * file. * * * * Removal or modification of this copyright notice is prohibited. * * * ******************************************************************************/ let converters = function () { let charToNibble = {}; let nibbleToChar = []; let i; for (i = 0; i <= 9; ++i) { let character = i.toString(); charToNibble[character] = i; nibbleToChar.push(character); } for (i = 10; i <= 15; ++i) { let lowerChar = String.fromCharCode('a'.charCodeAt(0) + i - 10); let upperChar = String.fromCharCode('A'.charCodeAt(0) + i - 10); charToNibble[lowerChar] = i; charToNibble[upperChar] = i; nibbleToChar.push(lowerChar); } return { byteArrayToHexString: function (bytes) { let str = ''; for (let i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) { bytes[i] += 256; } str += nibbleToChar[bytes[i] >> 4] + nibbleToChar[bytes[i] & 0x0F]; } return str; }, stringToByteArray: function (str): Array { str = unescape(encodeURIComponent(str)); let bytes = new Array(str.length); for (let i = 0; i < str.length; ++i) bytes[i] = str.charCodeAt(i); return bytes; }, hexStringToByteArray: function (str) { let bytes = []; let i = 0; if (0 !== str.length % 2) { bytes.push(charToNibble[str.charAt(0)]); ++i; } for (; i < str.length - 1; i += 2) bytes.push((charToNibble[str.charAt(i)] << 4) + charToNibble[str.charAt(i + 1)]); return bytes; }, stringToHexString: function (str) { return this.byteArrayToHexString(this.stringToByteArray(str)); }, hexStringToString: function (hex) { return this.byteArrayToString(this.hexStringToByteArray(hex)); }, checkBytesToIntInput: function (bytes, numBytes, opt_startIndex) { let startIndex = opt_startIndex || 0; if (startIndex < 0) { throw new Error('Start index should not be negative'); } if (bytes.length < startIndex + numBytes) { throw new Error('Need at least ' + (numBytes) + ' bytes to convert to an integer'); } return startIndex; }, byteArrayToSignedShort: function (bytes, opt_startIndex) { let index = this.checkBytesToIntInput(bytes, 2, opt_startIndex); let value = bytes[index]; value += bytes[index + 1] << 8; return value; }, byteArrayToSignedInt32: function (bytes, opt_startIndex) { let index = this.checkBytesToIntInput(bytes, 4, opt_startIndex); let value = bytes[index]; value += bytes[index + 1] << 8; value += bytes[index + 2] << 16; value += bytes[index + 3] << 24; return value; }, byteArrayToBigInteger: function (bytes, opt_startIndex) { let index = this.checkBytesToIntInput(bytes, 8, opt_startIndex); let value = new BigInteger('0', 10); let temp1, temp2; for (let i = 7; i >= 0; i--) { temp1 = value.multiply(new BigInteger('256', 10)); temp2 = temp1.add(new BigInteger(bytes[opt_startIndex + i].toString(10), 10)); value = temp2; } return value; }, // create a wordArray that is Big-Endian byteArrayToWordArray: function (byteArray) { let i = 0, offset = 0, word = 0, len = byteArray.length; let words = new Uint32Array(((len / 4) | 0) + (len % 4 == 0 ? 0 : 1)); while (i < (len - (len % 4))) { words[offset++] = (byteArray[i++] << 24) | (byteArray[i++] << 16) | (byteArray[i++] << 8) | (byteArray[i++]); } if (len % 4 != 0) { word = byteArray[i++] << 24; if (len % 4 > 1) { word = word | byteArray[i++] << 16; } if (len % 4 > 2) { word = word | byteArray[i++] << 8; } words[offset] = word; } let wordArray: any = new Object(); wordArray.sigBytes = len; wordArray.words = words; return wordArray; }, // assumes wordArray is Big-Endian wordArrayToByteArray: function (wordArray) { return converters.wordArrayToByteArrayImpl(wordArray, true); }, wordArrayToByteArrayImpl: function (wordArray, isFirstByteHasSign) { let len = wordArray.words.length; if (len == 0) { return new Array(0); } let byteArray = new Array(wordArray.sigBytes); let offset = 0, word, i; for (i = 0; i < len - 1; i++) { word = wordArray.words[i]; byteArray[offset++] = isFirstByteHasSign ? word >> 24 : (word >> 24) & 0xff; byteArray[offset++] = (word >> 16) & 0xff; byteArray[offset++] = (word >> 8) & 0xff; byteArray[offset++] = word & 0xff; } word = wordArray.words[len - 1]; byteArray[offset++] = isFirstByteHasSign ? word >> 24 : (word >> 24) & 0xff; if (wordArray.sigBytes % 4 == 0) { byteArray[offset++] = (word >> 16) & 0xff; byteArray[offset++] = (word >> 8) & 0xff; byteArray[offset++] = word & 0xff; } if (wordArray.sigBytes % 4 > 1) { byteArray[offset++] = (word >> 16) & 0xff; } if (wordArray.sigBytes % 4 > 2) { byteArray[offset++] = (word >> 8) & 0xff; } return byteArray; }, byteArrayToString: function (bytes, opt_startIndex?, length?) { if (length == 0) { return ''; } if (opt_startIndex && length) { let index = this.checkBytesToIntInput(bytes, parseInt(length, 10), parseInt(opt_startIndex, 10)); bytes = bytes.slice(opt_startIndex, opt_startIndex + length); } return decodeURIComponent(escape(String.fromCharCode.apply(null, bytes))); }, byteArrayToShortArray: function (byteArray) { let shortArray = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; let i; for (i = 0; i < 16; i++) { shortArray[i] = byteArray[i * 2] | byteArray[i * 2 + 1] << 8; } return shortArray; }, shortArrayToByteArray: function (shortArray) { let byteArray = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; let i; for (i = 0; i < 16; i++) { byteArray[2 * i] = shortArray[i] & 0xff; byteArray[2 * i + 1] = shortArray[i] >> 8; } return byteArray; }, shortArrayToHexString: function (ary) { let res = ''; for (let i = 0; i < ary.length; i++) { res += nibbleToChar[(ary[i] >> 4) & 0x0f] + nibbleToChar[ary[i] & 0x0f] + nibbleToChar[(ary[i] >> 12) & 0x0f] + nibbleToChar[(ary[i] >> 8) & 0x0f]; } return res; }, /** * Produces an array of the specified number of bytes to represent the integer * value. Default output encodes ints in little endian format. Handles signed * as well as unsigned integers. Due to limitations in JavaScript's number * format, x cannot be a true 64 bit integer (8 bytes). */ intToBytes_: function (x, numBytes, unsignedMax, opt_bigEndian) { let signedMax = Math.floor(unsignedMax / 2); let negativeMax = (signedMax + 1) * -1; if (x != Math.floor(x) || x < negativeMax || x > unsignedMax) { throw new Error( x + ' is not a ' + (numBytes * 8) + ' bit integer'); } let bytes = []; let current; // Number type 0 is in the positive int range, 1 is larger than signed int, // and 2 is negative int. let numberType = x >= 0 && x <= signedMax ? 0 : x > signedMax && x <= unsignedMax ? 1 : 2; if (numberType == 2) { x = (x * -1) - 1; } for (let i = 0; i < numBytes; i++) { if (numberType == 2) { current = 255 - (x % 256); } else { current = x % 256; } if (opt_bigEndian) { bytes.unshift(current); } else { bytes.push(current); } if (numberType == 1) { x = Math.floor(x / 256); } else { x = x >> 8; } } return bytes; }, int32ToBytes: function (x, opt_bigEndian) { return converters.intToBytes_(x, 4, 4294967295, opt_bigEndian); }, int16ToBytes: function (x, opt_bigEndian) { return converters.intToBytes_(x, 2, 65535, opt_bigEndian); }, /** * Based on https://groups.google.com/d/msg/crypto-js/TOb92tcJlU0/Eq7VZ5tpi-QJ * Converts a word array to a Uint8Array. * @param {WordArray} wordArray The word array. * @return {Uint8Array} The Uint8Array. */ wordArrayToByteArrayEx: function (wordArray) { // Shortcuts let words = wordArray.words; let sigBytes = wordArray.sigBytes; // Convert let u8 = new Uint8Array(sigBytes); for (let i = 0; i < sigBytes; i++) { let byte = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff; u8[i] = byte; } return u8; } }; }(); /** END OF THE LICENSED CODE */ export default converters;