简体   繁体   English

javascript可逆伪哈希

[英]javascript reversible pseudo hashing

Are there any "reversible" pseudo hashing functions available in javascript? javascript中有可用的“可逆”伪哈希函数吗? The reason I ask is because I'm bouncing a string around various parts of my application but the string contains characters that are incompatible with certain parts of the app. 我问的原因是因为我在应用程序的各个部分附近弹跳了一个字符串,但是该字符串包含与该应用程序的某些部分不兼容的字符。 So I'd like to convert "152(/,blue-Test#" in to an alphanumeric string and then be able to convert it back to the original value later on. 因此,我想将“ 152(/,blue-Test#”转换为字母数字字符串,然后稍后再将其转换回原始值。

Security etc is not important, it can be "crackable", the length of the hash can be variable, etc. 安全性等并不重要,它可以是“易破解的”,哈希的长度可以是可变的,等等。

If the same function could be easily replicated in to a PHP function that would be perfect. 如果可以轻松地将同一函数复制到PHP函数中,那将是完美的。

What about Base64 -Encoding? 那么Base64编码呢? There are many implementations out there. 有很多实现。 If it still contains undesirable characters (which is likely - 62<64), you can roll up one on your own and use only the characters for indexing that you can pass around safely. 如果它仍然包含不需要的字符(可能是-62 <64),则可以自己汇总一个字符,并仅使用可以安全传递的字符进行索引。

Or use Base32 . 或使用Base32

Have you considered HTML encoding the strings? 您是否考虑过对字符串进行HTML编码?

<?
    echo htmlspecialchars(someString);
    echo htmlspecialchars_decode("this -&gt; &quot;");
?> 

http://php.mirrors.ilisys.com.au/manual/en/function.htmlspecialchars.php http://php.mirrors.ilisys.com.au/manual/en/function.htmlspecialchars.php

This will let you pass strings around the app that would otherwise throw errors or inject code, and it is also reversible. 这将使您能够在应用程序周围传递字符串,否则将引发错误或注入代码,并且它也是可逆的。

It's possible that URI encoding is enough for you (depending on exactly which characters you find undesirable). URI编码可能足以满足您的要求(具体取决于您发现不希望使用的字符)。 In JavaScript: 在JavaScript中:

encodeURIComponent("152(/,blue-Test#")

Output: 输出:

"152(%2F%2Cblue-Test%23"

To reverse: 扭转:

decodeURIComponent("152(%2F%2Cblue-Test%23")

Output: 输出:

"152(/,blue-Test#"

If you're bouncing a lot of information (say, an array) around your app, you'd probably be best off turning it into a JSON object. 如果您在应用程序周围弹出很多信息(例如数组),则最好将其转换为JSON对象。 This lets you move information with structures in-tact. 这使您可以移动结构完整的信息。

http://www.json.org/js.html http://www.json.org/js.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM