在javascript中将会话UTF-8编码为1251
我需要将 UTF-8 中的字符串转换为 JavaScript 中 1251 代码页中的字节数组。谷歌说没有什么有用的。帮助:)
编辑:它的用途是什么。
我们有服务器和客户端。服务器具有使用 SHA512 和 1251 代码页的用户密码。客户端(Web 浏览器)必须使用 SHA512 对密码(由用户以纯文本形式提供)进行哈希处理并将其传输到服务器。问题是:浏览器以 UTF-8 编码工作,当然 UFT-8 中的 SHA512 与 1251 中的 SHA512 不同。有什么想法吗?
EDIT2:明白了!:)
//utf8 to 1251 converter (1 byte format, RU/EN support only + any other symbols) by drgluck
function utf8_decode (aa) {
var bb = '', c = 0;
for (var i = 0; i < aa.length; i++) {
c = aa.charCodeAt(i);
if (c > 127) {
if (c > 1024) {
if (c == 1025) {
c = 1016;
} else if (c == 1105) {
c = 1032;
}
bb += String.fromCharCode(c - 848);
}
} else {
bb += aa.charAt(i);
}
}
return bb;
}
Paul Johnston 的 SHA512 实现的用法 (http://pajhome.org.uk/crypt/md5/sha512.html)
...
return rstr2hex(rstr_sha512(utf8_decode(input)));
I need to convert string in UTF-8 to byte array in 1251 codepage in JavaScript. Google says nothing useful. Help :)
EDIT: What is it for.
We have server and client. Server has userpasswords hased with SHA512 and 1251 codepage. Client (web browser) has to hash passwords (provided by user in plain text) with SHA512 and transmit it to the server. The problem is: browser works in UTF-8 encoding, and of course SHA512 in UFT-8 is not the same as SHA512 in 1251. Any Ideas?
EDIT2: got it! :)
//utf8 to 1251 converter (1 byte format, RU/EN support only + any other symbols) by drgluck
function utf8_decode (aa) {
var bb = '', c = 0;
for (var i = 0; i < aa.length; i++) {
c = aa.charCodeAt(i);
if (c > 127) {
if (c > 1024) {
if (c == 1025) {
c = 1016;
} else if (c == 1105) {
c = 1032;
}
bb += String.fromCharCode(c - 848);
}
} else {
bb += aa.charAt(i);
}
}
return bb;
}
Usage with SHA512 implementation by Paul Johnston (http://pajhome.org.uk/crypt/md5/sha512.html)
...
return rstr2hex(rstr_sha512(utf8_decode(input)));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这会将 JavaScript 中的 Unicode 代码点编码为 windows-1251:
JavaScript 将每个字符存储为原始 Unicode 代码点,因此在使用 SHA512 获取哈希之前,上述操作应该有效。
This encodes the Unicode codepoints in JavaScript to windows-1251:
JavaScript stores each character as raw Unicode codepoints, so the above should work before getting the hash using SHA512.
当您在javascript中访问字符串时,它甚至不再是UTF-8编码(浏览器自动将其解码为内置的javascript编码或您在加载javascript或文本时指定的编码,或默认的浏览器编码),因此您需要将其转换先转为UTF-8,再转为cp1251。
看看这个 unicode编码转换器,我希望你能从中得到一些想法那。
When you access string in javascript, its not even UTF-8 encoding anymore (browsers automatically decode it to built-in javascript encoding OR the one you specified when you load javascript or text, OR default browser encoding), so you need to convert it to UTF-8 first and then convert to cp1251.
Take a look at this unicode encoding converter, I hope you'll get some ideas from that.
如果有人需要将编码字符串放入 url,则将之前答案中的代码修改为
In case if someone needs to put encoded string to url modify code from previous answer to
如果您应该猜测编码,对于俄语文本,您可以使用这个
If you should guess encoding, for Russian text you can use this
2021年
最后我们可以使用 TextEncoder() 和 TextDecoder()
Polyfill:
https://github.com/anonyco/FastestSmallestTextEncoderDecoder
2021
finally we can use TextEncoder() and TextDecoder()
Polyfill:
https://github.com/anonyco/FastestSmallestTextEncoderDecoder