UNPKG

tweetnacl-ts

Version:

Port of TweetNaCl cryptographic library to TypeScript (and ES6)

59 lines 1.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var array_1 = require("../array"); var validate_1 = require("../validate"); var fromCharCode = String.fromCharCode; function encodeUTF8(a) { return decodeURIComponent(escape(fromCharCode.apply(undefined, a))); } exports.encodeUTF8 = encodeUTF8; function decodeUTF8(s) { if (typeof s !== 'string') throw new TypeError('expected string'); var d = unescape(encodeURIComponent(s)), b = array_1.ByteArray(d.length); for (var i = 0; i < d.length; i++) { b[i] = d.charCodeAt(i); } return b; } exports.decodeUTF8 = decodeUTF8; function encodeBase64(a) { return btoa(fromCharCode.apply(undefined, a)); } exports.encodeBase64 = encodeBase64; function decodeBase64(s) { validate_1.validateBase64(s); var d = atob(s), b = array_1.ByteArray(d.length); for (var i = 0; i < d.length; i++) { b[i] = d.charCodeAt(i); } return b; } exports.decodeBase64 = decodeBase64; function put_hb(h) { return h + (h < 10 ? 48 : 87); } function encodeHex(a) { var b = array_1.ByteArray(a.length << 1); for (var i = 0, j = void 0; i < a.length; i++) { j = i << 1; b[j] = put_hb(a[i] >> 8); b[j + 1] = put_hb(a[i] & 0xf); } return fromCharCode.apply(undefined, b); } exports.encodeHex = encodeHex; function get_hb(s, i) { var c = s.charCodeAt(i); return c - (c < 58 ? 48 : c < 71 ? 55 : 87); } function decodeHex(s) { validate_1.validateHex(s); var a = array_1.ByteArray(s.length >> 1); for (var i = 0; i < a.length; i += 2) { a[i] = (get_hb(s, i) << 8) | get_hb(s, i + 1); } return a; } exports.decodeHex = decodeHex; //# sourceMappingURL=convert.js.map