开发者

javascript reversible pseudo hashing

开发者 https://www.devze.com 2023-02-26 04:53 出处:网络
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 st开发者_如何转开发ring

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 st开发者_如何转开发ring 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

0

精彩评论

暂无评论...
验证码 换一张
取 消