73 lines
1.9 KiB
JavaScript
73 lines
1.9 KiB
JavaScript
|
/*
|
||
|
CryptoJS v3.0.2
|
||
|
code.google.com/p/crypto-js
|
||
|
(c) 2009-2012 by Jeff Mott. All rights reserved.
|
||
|
code.google.com/p/crypto-js/wiki/License
|
||
|
*/
|
||
|
(function () {
|
||
|
// Shortcuts
|
||
|
var C = CryptoJS;
|
||
|
var C_lib = C.lib;
|
||
|
var WordArray = C_lib.WordArray;
|
||
|
var C_enc = C.enc;
|
||
|
|
||
|
/**
|
||
|
* UTF-16 BE encoding strategy.
|
||
|
*/
|
||
|
var Utf16 = C_enc.Utf16 = {
|
||
|
/**
|
||
|
* Converts a word array to a UTF-16 string.
|
||
|
*
|
||
|
* @param {WordArray} wordArray The word array.
|
||
|
*
|
||
|
* @return {string} The UTF-16 string.
|
||
|
*
|
||
|
* @static
|
||
|
*
|
||
|
* @example
|
||
|
*
|
||
|
* var utf16String = CryptoJS.enc.Utf16.stringify(wordArray);
|
||
|
*/
|
||
|
stringify: function (wordArray) {
|
||
|
// Shortcuts
|
||
|
var words = wordArray.words;
|
||
|
var sigBytes = wordArray.sigBytes;
|
||
|
|
||
|
// Convert
|
||
|
var utf16Chars = [];
|
||
|
for (var i = 0; i < sigBytes; i += 2) {
|
||
|
var codePoint = (words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff;
|
||
|
utf16Chars.push(String.fromCharCode(codePoint));
|
||
|
}
|
||
|
|
||
|
return utf16Chars.join('');
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* Converts a UTF-16 string to a word array.
|
||
|
*
|
||
|
* @param {string} utf16Str The UTF-16 string.
|
||
|
*
|
||
|
* @return {WordArray} The word array.
|
||
|
*
|
||
|
* @static
|
||
|
*
|
||
|
* @example
|
||
|
*
|
||
|
* var wordArray = CryptoJS.enc.Utf16.parse(utf16String);
|
||
|
*/
|
||
|
parse: function (utf16Str) {
|
||
|
// Shortcut
|
||
|
var utf16StrLength = utf16Str.length;
|
||
|
|
||
|
// Convert
|
||
|
var words = [];
|
||
|
for (var i = 0; i < utf16StrLength; i++) {
|
||
|
words[i >>> 1] |= utf16Str.charCodeAt(i) << (16 - (i % 2) * 16);
|
||
|
}
|
||
|
|
||
|
return WordArray.create(words, utf16StrLength * 2);
|
||
|
}
|
||
|
};
|
||
|
}());
|