简体   繁体   中英

javascript reversible pseudo hashing

Are there any "reversible" pseudo hashing functions available in 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.

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.

What about Base64 -Encoding? 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.

Or use Base32 .

Have you considered HTML encoding the strings?

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

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). In 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. This lets you move information with structures in-tact.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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