UNPKG

tweetnacl-ts

Version:

Port of TweetNaCl cryptographic library to TypeScript (and ES6)

51 lines 1.48 kB
import { ByteArray } from '../array'; import { validateBase64, validateHex } from '../validate'; var fromCharCode = String.fromCharCode; export function encodeUTF8(a) { return decodeURIComponent(escape(fromCharCode.apply(undefined, a))); } export function decodeUTF8(s) { if (typeof s !== 'string') throw new TypeError('expected string'); var d = unescape(encodeURIComponent(s)), b = ByteArray(d.length); for (var i = 0; i < d.length; i++) { b[i] = d.charCodeAt(i); } return b; } export function encodeBase64(a) { return btoa(fromCharCode.apply(undefined, a)); } export function decodeBase64(s) { validateBase64(s); var d = atob(s), b = ByteArray(d.length); for (var i = 0; i < d.length; i++) { b[i] = d.charCodeAt(i); } return b; } function put_hb(h) { return h + (h < 10 ? 48 : 87); } export function encodeHex(a) { var b = 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); } function get_hb(s, i) { var c = s.charCodeAt(i); return c - (c < 58 ? 48 : c < 71 ? 55 : 87); } export function decodeHex(s) { validateHex(s); var a = 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; } //# sourceMappingURL=convert.js.map