| 1 |
/*--------------------------------------------------------------------------------------------- |
| 2 |
* Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
* Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 |
*--------------------------------------------------------------------------------------------*/ |
| 5 |
|
| 6 |
import * as strings from 'vs/base/common/strings'; |
| 7 |
|
| 8 |
/** |
| 9 |
* Return a hash value for an object. |
| 10 |
*/ |
| 11 |
export function hash(obj: any): number { |
| 12 |
return doHash(obj, 0); |
| 13 |
} |
| 14 |
|
| 15 |
export function doHash(obj: any, hashVal: number): number { |
| 16 |
switch (typeof obj) { |
| 17 |
case 'object': |
| 18 |
if (obj === null) { |
| 19 |
return numberHash(349, hashVal); |
| 20 |
} else if (Array.isArray(obj)) { |
| 21 |
return arrayHash(obj, hashVal); |
| 22 |
} |
| 23 |
return objectHash(obj, hashVal); |
| 24 |
case 'string': |
| 25 |
return stringHash(obj, hashVal); |
| 26 |
case 'boolean': |
| 27 |
return booleanHash(obj, hashVal); |
| 28 |
case 'number': |
| 29 |
return numberHash(obj, hashVal); |
| 30 |
case 'undefined': |
| 31 |
return numberHash(937, hashVal); |
| 32 |
default: |
| 33 |
return numberHash(617, hashVal); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
export function numberHash(val: number, initialHashVal: number): number { |
| 38 |
return (((initialHashVal << 5) - initialHashVal) + val) | 0; // hashVal * 31 + ch, keep as int32 |
| 39 |
} |
| 40 |
|
| 41 |
function booleanHash(b: boolean, initialHashVal: number): number { |
| 42 |
return numberHash(b ? 433 : 863, initialHashVal); |
| 43 |
} |
| 44 |
|
| 45 |
export function stringHash(s: string, hashVal: number) { |
| 46 |
hashVal = numberHash(149417, hashVal); |
| 47 |
for (let i = 0, length = s.length; i < length; i++) { |
| 48 |
hashVal = numberHash(s.charCodeAt(i), hashVal); |
| 49 |
} |
| 50 |
return hashVal; |
| 51 |
} |
| 52 |
|
| 53 |
function arrayHash(arr: any[], initialHashVal: number): number { |
| 54 |
initialHashVal = numberHash(104579, initialHashVal); |
| 55 |
return arr.reduce((hashVal, item) => doHash(item, hashVal), initialHashVal); |
| 56 |
} |
| 57 |
|
| 58 |
function objectHash(obj: any, initialHashVal: number): number { |
| 59 |
initialHashVal = numberHash(181387, initialHashVal); |
| 60 |
return Object.keys(obj).sort().reduce((hashVal, key) => { |
| 61 |
hashVal = stringHash(key, hashVal); |
| 62 |
return doHash(obj[key], hashVal); |
| 63 |
}, initialHashVal); |
| 64 |
} |
| 65 |
|
| 66 |
export class Hasher { |
| 67 |
|
| 68 |
private _value = 0; |
| 69 |
|
| 70 |
get value(): number { |
| 71 |
return this._value; |
| 72 |
} |
| 73 |
|
| 74 |
hash(obj: any): number { |
| 75 |
this._value = doHash(obj, this._value); |
| 76 |
return this._value; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
const enum SHA1Constant { |
| 81 |
BLOCK_SIZE = 64, // 512 / 8 |
| 82 |
UNICODE_REPLACEMENT = 0xFFFD, |
| 83 |
} |
| 84 |
|
| 85 |
function leftRotate(value: number, bits: number, totalBits: number = 32): number { |
| 86 |
// delta + bits = totalBits |
| 87 |
const delta = totalBits - bits; |
| 88 |
|
| 89 |
// All ones, expect `delta` zeros aligned to the right |
| 90 |
const mask = ~((1 << delta) - 1); |
| 91 |
|
| 92 |
// Join (value left-shifted `bits` bits) with (masked value right-shifted `delta` bits) |
| 93 |
return ((value << bits) | ((mask & value) >>> delta)) >>> 0; |
| 94 |
} |
| 95 |
|
| 96 |
function fill(dest: Uint8Array, index: number = 0, count: number = dest.byteLength, value: number = 0): void { |
| 97 |
for (let i = 0; i < count; i++) { |
| 98 |
dest[index + i] = value; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
function leftPad(value: string, length: number, char: string = '0'): string { |
| 103 |
while (value.length < length) { |
| 104 |
value = char + value; |
| 105 |
} |
| 106 |
return value; |
| 107 |
} |
| 108 |
|
| 109 |
export function toHexString(buffer: ArrayBuffer): string; |
| 110 |
export function toHexString(value: number, bitsize?: number): string; |
| 111 |
export function toHexString(bufferOrValue: ArrayBuffer | number, bitsize: number = 32): string { |
| 112 |
if (bufferOrValue instanceof ArrayBuffer) { |
| 113 |
return Array.from(new Uint8Array(bufferOrValue)).map(b => b.toString(16).padStart(2, '0')).join(''); |
| 114 |
} |
| 115 |
|
| 116 |
return leftPad((bufferOrValue >>> 0).toString(16), bitsize / 4); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* A SHA1 implementation that works with strings and does not allocate. |
| 121 |
*/ |
| 122 |
export class StringSHA1 { |
| 123 |
private static _bigBlock32 = new DataView(new ArrayBuffer(320)); // 80 * 4 = 320 |
| 124 |
|
| 125 |
private _h0 = 0x67452301; |
| 126 |
private _h1 = 0xEFCDAB89; |
| 127 |
private _h2 = 0x98BADCFE; |
| 128 |
private _h3 = 0x10325476; |
| 129 |
private _h4 = 0xC3D2E1F0; |
| 130 |
|
| 131 |
private readonly _buff: Uint8Array; |
| 132 |
private readonly _buffDV: DataView; |
| 133 |
private _buffLen: number; |
| 134 |
private _totalLen: number; |
| 135 |
private _leftoverHighSurrogate: number; |
| 136 |
private _finished: boolean; |
| 137 |
|
| 138 |
constructor() { |
| 139 |
this._buff = new Uint8Array(SHA1Constant.BLOCK_SIZE + 3 /* to fit any utf-8 */); |
| 140 |
this._buffDV = new DataView(this._buff.buffer); |
| 141 |
this._buffLen = 0; |
| 142 |
this._totalLen = 0; |
| 143 |
this._leftoverHighSurrogate = 0; |
| 144 |
this._finished = false; |
| 145 |
} |
| 146 |
|
| 147 |
public update(str: string): void { |
| 148 |
const strLen = str.length; |
| 149 |
if (strLen === 0) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
const buff = this._buff; |
| 154 |
let buffLen = this._buffLen; |
| 155 |
let leftoverHighSurrogate = this._leftoverHighSurrogate; |
| 156 |
let charCode: number; |
| 157 |
let offset: number; |
| 158 |
|
| 159 |
if (leftoverHighSurrogate !== 0) { |
| 160 |
charCode = leftoverHighSurrogate; |
| 161 |
offset = -1; |
| 162 |
leftoverHighSurrogate = 0; |
| 163 |
} else { |
| 164 |
charCode = str.charCodeAt(0); |
| 165 |
offset = 0; |
| 166 |
} |
| 167 |
|
| 168 |
while (true) { |
| 169 |
let codePoint = charCode; |
| 170 |
if (strings.isHighSurrogate(charCode)) { |
| 171 |
if (offset + 1 < strLen) { |
| 172 |
const nextCharCode = str.charCodeAt(offset + 1); |
| 173 |
if (strings.isLowSurrogate(nextCharCode)) { |
| 174 |
offset++; |
| 175 |
codePoint = strings.computeCodePoint(charCode, nextCharCode); |
| 176 |
} else { |
| 177 |
// illegal => unicode replacement character |
| 178 |
codePoint = SHA1Constant.UNICODE_REPLACEMENT; |
| 179 |
} |
| 180 |
} else { |
| 181 |
// last character is a surrogate pair |
| 182 |
leftoverHighSurrogate = charCode; |
| 183 |
break; |
| 184 |
} |
| 185 |
} else if (strings.isLowSurrogate(charCode)) { |
| 186 |
// illegal => unicode replacement character |
| 187 |
codePoint = SHA1Constant.UNICODE_REPLACEMENT; |
| 188 |
} |
| 189 |
|
| 190 |
buffLen = this._push(buff, buffLen, codePoint); |
| 191 |
offset++; |
| 192 |
if (offset < strLen) { |
| 193 |
charCode = str.charCodeAt(offset); |
| 194 |
} else { |
| 195 |
break; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
this._buffLen = buffLen; |
| 200 |
this._leftoverHighSurrogate = leftoverHighSurrogate; |
| 201 |
} |
| 202 |
|
| 203 |
private _push(buff: Uint8Array, buffLen: number, codePoint: number): number { |
| 204 |
if (codePoint < 0x0080) { |
| 205 |
buff[buffLen++] = codePoint; |
| 206 |
} else if (codePoint < 0x0800) { |
| 207 |
buff[buffLen++] = 0b11000000 | ((codePoint & 0b00000000000000000000011111000000) >>> 6); |
| 208 |
buff[buffLen++] = 0b10000000 | ((codePoint & 0b00000000000000000000000000111111) >>> 0); |
| 209 |
} else if (codePoint < 0x10000) { |
| 210 |
buff[buffLen++] = 0b11100000 | ((codePoint & 0b00000000000000001111000000000000) >>> 12); |
| 211 |
buff[buffLen++] = 0b10000000 | ((codePoint & 0b00000000000000000000111111000000) >>> 6); |
| 212 |
buff[buffLen++] = 0b10000000 | ((codePoint & 0b00000000000000000000000000111111) >>> 0); |
| 213 |
} else { |
| 214 |
buff[buffLen++] = 0b11110000 | ((codePoint & 0b00000000000111000000000000000000) >>> 18); |
| 215 |
buff[buffLen++] = 0b10000000 | ((codePoint & 0b00000000000000111111000000000000) >>> 12); |
| 216 |
buff[buffLen++] = 0b10000000 | ((codePoint & 0b00000000000000000000111111000000) >>> 6); |
| 217 |
buff[buffLen++] = 0b10000000 | ((codePoint & 0b00000000000000000000000000111111) >>> 0); |
| 218 |
} |
| 219 |
|
| 220 |
if (buffLen >= SHA1Constant.BLOCK_SIZE) { |
| 221 |
this._step(); |
| 222 |
buffLen -= SHA1Constant.BLOCK_SIZE; |
| 223 |
this._totalLen += SHA1Constant.BLOCK_SIZE; |
| 224 |
// take last 3 in case of UTF8 overflow |
| 225 |
buff[0] = buff[SHA1Constant.BLOCK_SIZE + 0]; |
| 226 |
buff[1] = buff[SHA1Constant.BLOCK_SIZE + 1]; |
| 227 |
buff[2] = buff[SHA1Constant.BLOCK_SIZE + 2]; |
| 228 |
} |
| 229 |
|
| 230 |
return buffLen; |
| 231 |
} |
| 232 |
|
| 233 |
public digest(): string { |
| 234 |
if (!this._finished) { |
| 235 |
this._finished = true; |
| 236 |
if (this._leftoverHighSurrogate) { |
| 237 |
// illegal => unicode replacement character |
| 238 |
this._leftoverHighSurrogate = 0; |
| 239 |
this._buffLen = this._push(this._buff, this._buffLen, SHA1Constant.UNICODE_REPLACEMENT); |
| 240 |
} |
| 241 |
this._totalLen += this._buffLen; |
| 242 |
this._wrapUp(); |
| 243 |
} |
| 244 |
|
| 245 |
return toHexString(this._h0) + toHexString(this._h1) + toHexString(this._h2) + toHexString(this._h3) + toHexString(this._h4); |
| 246 |
} |
| 247 |
|
| 248 |
private _wrapUp(): void { |
| 249 |
this._buff[this._buffLen++] = 0x80; |
| 250 |
fill(this._buff, this._buffLen); |
| 251 |
|
| 252 |
if (this._buffLen > 56) { |
| 253 |
this._step(); |
| 254 |
fill(this._buff); |
| 255 |
} |
| 256 |
|
| 257 |
// this will fit because the mantissa can cover up to 52 bits |
| 258 |
const ml = 8 * this._totalLen; |
| 259 |
|
| 260 |
this._buffDV.setUint32(56, Math.floor(ml / 4294967296), false); |
| 261 |
this._buffDV.setUint32(60, ml % 4294967296, false); |
| 262 |
|
| 263 |
this._step(); |
| 264 |
} |
| 265 |
|
| 266 |
private _step(): void { |
| 267 |
const bigBlock32 = StringSHA1._bigBlock32; |
| 268 |
const data = this._buffDV; |
| 269 |
|
| 270 |
for (let j = 0; j < 64 /* 16*4 */; j += 4) { |
| 271 |
bigBlock32.setUint32(j, data.getUint32(j, false), false); |
| 272 |
} |
| 273 |
|
| 274 |
for (let j = 64; j < 320 /* 80*4 */; j += 4) { |
| 275 |
bigBlock32.setUint32(j, leftRotate((bigBlock32.getUint32(j - 12, false) ^ bigBlock32.getUint32(j - 32, false) ^ bigBlock32.getUint32(j - 56, false) ^ bigBlock32.getUint32(j - 64, false)), 1), false); |
| 276 |
} |
| 277 |
|
| 278 |
let a = this._h0; |
| 279 |
let b = this._h1; |
| 280 |
let c = this._h2; |
| 281 |
let d = this._h3; |
| 282 |
let e = this._h4; |
| 283 |
|
| 284 |
let f: number, k: number; |
| 285 |
let temp: number; |
| 286 |
|
| 287 |
for (let j = 0; j < 80; j++) { |
| 288 |
if (j < 20) { |
| 289 |
f = (b & c) | ((~b) & d); |
| 290 |
k = 0x5A827999; |
| 291 |
} else if (j < 40) { |
| 292 |
f = b ^ c ^ d; |
| 293 |
k = 0x6ED9EBA1; |
| 294 |
} else if (j < 60) { |
| 295 |
f = (b & c) | (b & d) | (c & d); |
| 296 |
k = 0x8F1BBCDC; |
| 297 |
} else { |
| 298 |
f = b ^ c ^ d; |
| 299 |
k = 0xCA62C1D6; |
| 300 |
} |
| 301 |
|
| 302 |
temp = (leftRotate(a, 5) + f + e + k + bigBlock32.getUint32(j * 4, false)) & 0xffffffff; |
| 303 |
e = d; |
| 304 |
d = c; |
| 305 |
c = leftRotate(b, 30); |
| 306 |
b = a; |
| 307 |
a = temp; |
| 308 |
} |
| 309 |
|
| 310 |
this._h0 = (this._h0 + a) & 0xffffffff; |
| 311 |
this._h1 = (this._h1 + b) & 0xffffffff; |
| 312 |
this._h2 = (this._h2 + c) & 0xffffffff; |
| 313 |
this._h3 = (this._h3 + d) & 0xffffffff; |
| 314 |
this._h4 = (this._h4 + e) & 0xffffffff; |
| 315 |
} |
| 316 |
} |