Welcome to mirror list, hosted at ThFree Co, Russian Federation.

hash.ts « bits « common « base « vs « src - github.com/microsoft/vscode.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3d8ba064112e983e25a87953d7074102f0b828cb (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import platform = require('vs/base/common/platform');
import bits = require('vs/base/common/bits/bits');
import encoding = require('vs/base/common/bits/encoding');
import types = require('vs/base/common/types');

function copy(dest: Uint8Array, destIndex: number, src: Uint8Array, srcIndex: number, count: number): number {
	for (var i = 0, len = Math.min(dest.byteLength - destIndex, src.byteLength - srcIndex, count); i < len; i++) {
		dest[destIndex + i] = src[srcIndex + i];
	}

	return len;
}

function fill(dest: Uint8Array, index: number = 0, count: number = dest.byteLength, value: number = 0): void {
	for (var i = 0; i < count; i++) {
		dest[index + i] = value;
	}
}

export class SHA1 {

	// Reference: http://en.wikipedia.org/wiki/SHA-1

	private static BLOCK_SIZE = 64; // 512 / 8

	private length: number;
	private buffer: Uint8Array;
	private bufferDV: DataView;
	private bufferLength: number;

	private bigBlock32: DataView;
	private h0 = 0x67452301;
	private h1 = 0xEFCDAB89;
	private h2 = 0x98BADCFE;
	private h3 = 0x10325476;
	private h4 = 0xC3D2E1F0;

	public static digest(data: string): string;
	public static digest(data: Uint8Array): string;
	public static digest(data: ArrayBuffer): string;
	public static digest(data: DataView): string;
	public static digest(data: any): string
	{
		var sha = new SHA1();
		sha.update(data);
		return sha.digest();
	}

	constructor() {
		this.length = 0;

		this.buffer = new Uint8Array(SHA1.BLOCK_SIZE);
		this.bufferDV = new DataView(this.buffer.buffer);
		this.bufferLength = 0;

		this.bigBlock32 = new DataView(new ArrayBuffer(320)); // 80 * 4 = 320;
	}

	public update(data: string): void;
	public update(data: Uint8Array): void;
	public update(data: ArrayBuffer): void;
	public update(data: DataView): void;
	public update(arg: any): void {
		if (!this.buffer) {
			throw new Error('Digest already computed.');
		}

		var data: Uint8Array;

		if (types.isString(arg)) {
			data = new Uint8Array(encoding.encodeToUTF8(<string> arg));
		} else if (arg instanceof ArrayBuffer) {
			data = new Uint8Array(arg);
		} else if (arg instanceof DataView) {
			data = new Uint8Array((<DataView> arg).buffer);
		} else {
			data = <Uint8Array> arg;
		}

		var bytesRead = 0, totalBytesRead = 0;

		while (totalBytesRead < data.byteLength) {
			bytesRead = copy(this.buffer, this.bufferLength, data, totalBytesRead, data.byteLength);

			this.bufferLength += bytesRead;
			totalBytesRead += bytesRead;

			if (this.bufferLength === SHA1.BLOCK_SIZE) {
				this.step(this.bufferDV);
				this.bufferLength = 0;
			}
		}

		this.length += totalBytesRead;
	}

	public digest(): string {
		if (this.buffer) {
			this.wrapUp();
		}

		return bits.toHexString(this.h0) + bits.toHexString(this.h1) + bits.toHexString(this.h2) + bits.toHexString(this.h3) + bits.toHexString(this.h4);
	}

	private wrapUp(): void {
		this.buffer[this.bufferLength++] = 0x80;
		fill(this.buffer, this.bufferLength);

		if (this.bufferLength > 56) {
			this.step(this.bufferDV);
			fill(this.buffer);
		}

		var ml = bits.multiply64(8, this.length);
		this.bufferDV.setUint32(56, ml[0], false);
		this.bufferDV.setUint32(60, ml[1], false);

		this.step(this.bufferDV);

		this.buffer = null;
		this.bufferDV = null;
		this.bufferLength = -1;
	}

	private step(data: DataView): void {
		for (var j = 0; j < 64 /* 16*4 */; j += 4) {
			this.bigBlock32.setUint32(j, data.getUint32(j, false), false);
		}

		for (j = 64; j < 320 /* 80*4 */; j += 4) {
			this.bigBlock32.setUint32(j, bits.leftRotate((this.bigBlock32.getUint32(j - 12, false) ^ this.bigBlock32.getUint32(j - 32, false) ^ this.bigBlock32.getUint32(j - 56, false) ^ this.bigBlock32.getUint32(j - 64, false)), 1), false);
		}

		var a = this.h0;
		var b = this.h1;
		var c = this.h2;
		var d = this.h3;
		var e = this.h4;

		var f: number, k: number;
		var temp: number;

		for (j = 0; j < 80; j++) {
			if (j < 20) {
				f = (b & c) | ((~b) & d);
				k = 0x5A827999;
			} else if (j < 40) {
				f = b ^ c ^ d;
				k = 0x6ED9EBA1;
			} else if (j < 60) {
				f = (b & c) | (b & d) | (c & d);
				k = 0x8F1BBCDC;
			} else {
				f = b ^ c ^ d;
				k = 0xCA62C1D6;
			}

			temp = (bits.leftRotate(a, 5) + f + e + k + this.bigBlock32.getUint32(j * 4, false)) & 0xffffffff;
			e = d;
			d = c;
			c = bits.leftRotate(b, 30);
			b = a;
			a = temp;
		}

		this.h0 = (this.h0 + a) & 0xffffffff;
		this.h1 = (this.h1 + b) & 0xffffffff;
		this.h2 = (this.h2 + c) & 0xffffffff;
		this.h3 = (this.h3 + d) & 0xffffffff;
		this.h4 = (this.h4 + e) & 0xffffffff;
	}
}

export function computeSHA1Hash(value:string, hasBOM:boolean, headerFn?:(length:number)=>string):string;
export function computeSHA1Hash(value:ArrayBuffer, hasBOM:boolean, headerFn?:(length:number)=>string):string;
export function computeSHA1Hash(value:any, hasBOM:boolean, headerFn?:(length:number)=>string):string {
	if (typeof(ArrayBuffer) === 'undefined') {
		return null; // IE9 does not know ArrayBuffer
	}

	var data = types.isString(value) ? encoding.encodeToUTF8(value, hasBOM) : <ArrayBuffer>value;
	var hash = new SHA1();

	if (headerFn) {
		hash.update(headerFn(data.byteLength));
	}

	if (data.byteLength) {
		hash.update(data);
	}

	return hash.digest();
}