目录

js生成hash值

目录

http://image.catbro.cn/upload_c324756eb6150993dca313f7df4d7bb1.png

  • 记录一个js生成hash值的代码段
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
 * Jenkins hash implementation which yeilds 32-bit and 64-bit hashes.
 *
 * See https://github.com/vkandy/jenkins-hash-js
 */
class Jenkins {

    constructor() {
        /**
         * Default first initial seed.
         */
        this.pc = 0;

        /**
         * Default second initial seed.
         */
        this.pb = 0;
    }

    // --------------------------------------------------
    // Public access
    // --------------------------------------------------

    /**
     * Computes and returns 32-bit hash of given message.
     */
    hash32(msg) {
        var h = lookup3(msg, this.pc, this.pb);
        return (h.c).toString(16);
    }

    hash32ToBinary(msg) {
        var h = lookup3(msg, this.pc, this.pb);
        return (h.c).toString(2);
    }

    /**
     * Computes and returns 32-bit hash of given message.
     */
    hash64(msg) {
        var h = lookup3(msg, this.pc, this.pb);
        return (h.b).toString(16) + (h.c).toString(16);
    }

    hash64ToBinary(msg) {
        var h = lookup3(msg, this.pc, this.pb);
        return (h.b).toString(2) + (h.c).toString(2);
    }
}

/**
 * Implementation of lookup3 algorithm.
 */
function lookup3(k, pc, pb) {
    var length = k.length;
    var a, b, c;

    a = b = c = 0xdeadbeef + length + pc;
    c += pb;

    var offset = 0;
    while (length > 12) {
        a += k.charCodeAt(offset + 0);
        a += k.charCodeAt(offset + 1) << 8;
        a += k.charCodeAt(offset + 2) << 16;
        a += k.charCodeAt(offset + 3) << 24;

        b += k.charCodeAt(offset + 4);
        b += k.charCodeAt(offset + 5) << 8;
        b += k.charCodeAt(offset + 6) << 16;
        b += k.charCodeAt(offset + 7) << 24;

        c += k.charCodeAt(offset + 8);
        c += k.charCodeAt(offset + 9) << 8;
        c += k.charCodeAt(offset + 10) << 16;
        c += k.charCodeAt(offset + 11) << 24;

        mixed = mix(a, b, c);
        a = mixed.a;
        b = mixed.b;
        c = mixed.c;

        length -= 12;
        offset += 12;
    }

    switch (length) {
        case 12:
            c += k.charCodeAt(offset + 11) << 24;
        case 11:
            c += k.charCodeAt(offset + 10) << 16;
        case 10:
            c += k.charCodeAt(offset + 9) << 8;
        case 9:
            c += k.charCodeAt(offset + 8);

        case 8:
            b += k.charCodeAt(offset + 7) << 24;
        case 7:
            b += k.charCodeAt(offset + 6) << 16;
        case 6:
            b += k.charCodeAt(offset + 5) << 8;
        case 5:
            b += k.charCodeAt(offset + 4);

        case 4:
            a += k.charCodeAt(offset + 3) << 24;
        case 3:
            a += k.charCodeAt(offset + 2) << 16;
        case 2:
            a += k.charCodeAt(offset + 1) << 8;
        case 1:
            a += k.charCodeAt(offset + 0);
            break;

        case 0:
            return { c: c >>> 0, b: b >>> 0 };
    }

    // Final mixing of three 32-bit values in to c
    mixed = finalMix(a, b, c)
    a = mixed.a;
    b = mixed.b;
    c = mixed.c;

    return { c: c >>> 0, b: b >>> 0 };
};

/**
 * Mixes 3 32-bit integers reversibly but fast.
 */
function mix(a, b, c) {
    a -= c;
    a ^= rot(c, 4);
    c += b;
    b -= a;
    b ^= rot(a, 6);
    a += c;
    c -= b;
    c ^= rot(b, 8);
    b += a;
    a -= c;
    a ^= rot(c, 16);
    c += b;
    b -= a;
    b ^= rot(a, 19);
    a += c;
    c -= b;
    c ^= rot(b, 4);
    b += a;
    return { a: a, b: b, c: c };
};

/**
 * Final mixing of 3 32-bit values (a,b,c) into c
 */
function finalMix(a, b, c) {
    c ^= b;
    c -= rot(b, 14);
    a ^= c;
    a -= rot(c, 11);
    b ^= a;
    b -= rot(a, 25);
    c ^= b;
    c -= rot(b, 16);
    a ^= c;
    a -= rot(c, 4);
    b ^= a;
    b -= rot(a, 14);
    c ^= b;
    c -= rot(b, 24);
    return { a: a, b: b, c: c };
};

/**
 * Rotate x by k distance.
 */
function rot(x, k) {
    return (((x) << (k)) | ((x) >> (32 - (k))));
};

module.exports.Jenkins = Jenkins;