crypto-es
Version:
A cryptography algorithms library compatible with ES6 and TypeScript
59 lines (57 loc) • 1.7 kB
JavaScript
const require_core = require('./core.cjs');
//#region src/enc-utf16.ts
/**
* Swaps endian of a word
* @param word - The word to swap
* @returns The word with swapped endian
*/
const swapEndian = (word) => word << 8 & 4278255360 | word >>> 8 & 16711935;
/**
* UTF-16 BE encoding strategy.
*/
const Utf16BE = {
stringify(wordArray) {
const { words, sigBytes } = wordArray;
const utf16Chars = [];
for (let i = 0; i < sigBytes; i += 2) {
const codePoint = words[i >>> 2] >>> 16 - i % 4 * 8 & 65535;
utf16Chars.push(String.fromCharCode(codePoint));
}
return utf16Chars.join("");
},
parse(utf16Str) {
const utf16StrLength = utf16Str.length;
const words = [];
for (let i = 0; i < utf16StrLength; i += 1) words[i >>> 1] |= utf16Str.charCodeAt(i) << 16 - i % 2 * 16;
return require_core.WordArray.create(words, utf16StrLength * 2);
}
};
/**
* UTF-16 encoding strategy (defaults to UTF-16 BE).
*/
const Utf16 = Utf16BE;
/**
* UTF-16 LE encoding strategy.
*/
const Utf16LE = {
stringify(wordArray) {
const { words, sigBytes } = wordArray;
const utf16Chars = [];
for (let i = 0; i < sigBytes; i += 2) {
const codePoint = swapEndian(words[i >>> 2] >>> 16 - i % 4 * 8 & 65535);
utf16Chars.push(String.fromCharCode(codePoint));
}
return utf16Chars.join("");
},
parse(utf16Str) {
const utf16StrLength = utf16Str.length;
const words = [];
for (let i = 0; i < utf16StrLength; i += 1) words[i >>> 1] |= swapEndian(utf16Str.charCodeAt(i) << 16 - i % 2 * 16);
return require_core.WordArray.create(words, utf16StrLength * 2);
}
};
//#endregion
exports.Utf16 = Utf16;
exports.Utf16BE = Utf16BE;
exports.Utf16LE = Utf16LE;
//# sourceMappingURL=enc-utf16.cjs.map