stef
stef

Reputation: 27759

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.

Upvotes: 2

Views: 1801

Answers (4)

Winfield Trail
Winfield Trail

Reputation: 5695

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

Upvotes: 0

Wayne
Wayne

Reputation: 60414

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#"

Upvotes: 1

Alexander Gessler
Alexander Gessler

Reputation: 46627

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.

Upvotes: 4

Tom Gullen
Tom Gullen

Reputation: 61735

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.

Upvotes: 1

Related Questions