如何从字符串中获取哈希值,然后在javascript中从哈希值中获取字符串

时间:2013-08-26 12:36:38

标签: javascript string hash hashcode

我有一个非常长的(~70,000个字符)字符串,我想插入到URL中。我需要在浏览器中实现后退,所以当URL更改时,我的应用会做出反应并改变它的状态。

这是我用来从字符串生成哈希码的函数:

String.prototype.hashCode = function () {
            var hash = 0, i, char;
            if (this.length == 0) return hash;
            var l = this.length;
            for (i = 0; i < l; i++) {
                char = this.charCodeAt(i);
                hash = ((hash << 5) - hash) + char;
                hash |= 0; // Convert to 32bit integer
            }
            return hash;
        };

但是我怎样才能从它的哈希中取回我的字符串?

编辑:还有其他方法来压缩如此长的网址吗?

3 个答案:

答案 0 :(得分:4)

你不能。哈希是一种单向函数。 560,000位不能转换成32位再返回。

答案 1 :(得分:3)

魔术! (说不可能)

答案 2 :(得分:0)

根据您的使用情况,您可能使用了网址缩短服务来压缩您的网址。看看这个例子:https://twitter.com/peterjaric/status/336941762838945792(我使用tinyurl压缩长数据 - uris)。

指向tinyurl-page的直接链接:http://tinyurl.com/peterjaric-page1

相关问题