Skip to content

Commit 8ad5ac5

Browse files
authored
debt - add crypto.d.ts file defining the crypto-global that's shared between nodejs and browsers (#237465)
1 parent 23b62bc commit 8ad5ac5

File tree

3 files changed

+85
-75
lines changed

3 files changed

+85
-75
lines changed

src/typings/crypto.d.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
// NOTE that this is a partial copy from lib.dom.d.ts which is NEEDED because these utils are used in the /common/
7+
// layer which has no dependency on the DOM/browser-context. However, `crypto` is available as global in all browsers and
8+
// in nodejs. Therefore it's OK to spell out its typings here
9+
10+
declare global {
11+
12+
/**
13+
* This Web Crypto API interface provides a number of low-level cryptographic functions. It is accessed via the Crypto.subtle properties available in a window context (via Window.crypto).
14+
* Available only in secure contexts.
15+
*
16+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto)
17+
*/
18+
interface SubtleCrypto {
19+
// /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/decrypt) */
20+
// decrypt(algorithm: AlgorithmIdentifier | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams, key: CryptoKey, data: BufferSource): Promise<ArrayBuffer>;
21+
// /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/deriveBits) */
22+
// deriveBits(algorithm: AlgorithmIdentifier | EcdhKeyDeriveParams | HkdfParams | Pbkdf2Params, baseKey: CryptoKey, length?: number | null): Promise<ArrayBuffer>;
23+
// /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/deriveKey) */
24+
// deriveKey(algorithm: AlgorithmIdentifier | EcdhKeyDeriveParams | HkdfParams | Pbkdf2Params, baseKey: CryptoKey, derivedKeyType: AlgorithmIdentifier | AesDerivedKeyParams | HmacImportParams | HkdfParams | Pbkdf2Params, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
25+
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/digest) */
26+
digest(algorithm: { name: string } | string, data: ArrayBufferView | ArrayBuffer): Promise<ArrayBuffer>;
27+
// /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/encrypt) */
28+
// encrypt(algorithm: AlgorithmIdentifier | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams, key: CryptoKey, data: BufferSource): Promise<ArrayBuffer>;
29+
// /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) */
30+
// exportKey(format: "jwk", key: CryptoKey): Promise<JsonWebKey>;
31+
// exportKey(format: Exclude<KeyFormat, "jwk">, key: CryptoKey): Promise<ArrayBuffer>;
32+
// exportKey(format: KeyFormat, key: CryptoKey): Promise<ArrayBuffer | JsonWebKey>;
33+
// /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) */
34+
// generateKey(algorithm: "Ed25519", extractable: boolean, keyUsages: ReadonlyArray<"sign" | "verify">): Promise<CryptoKeyPair>;
35+
// generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKeyPair>;
36+
// generateKey(algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKey>;
37+
// generateKey(algorithm: AlgorithmIdentifier, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair | CryptoKey>;
38+
// /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/importKey) */
39+
// importKey(format: "jwk", keyData: JsonWebKey, algorithm: AlgorithmIdentifier | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | AesKeyAlgorithm, extractable: boolean, keyUsages: ReadonlyArray<KeyUsage>): Promise<CryptoKey>;
40+
// importKey(format: Exclude<KeyFormat, "jwk">, keyData: BufferSource, algorithm: AlgorithmIdentifier | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | AesKeyAlgorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
41+
// /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/sign) */
42+
// sign(algorithm: AlgorithmIdentifier | RsaPssParams | EcdsaParams, key: CryptoKey, data: BufferSource): Promise<ArrayBuffer>;
43+
// /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/unwrapKey) */
44+
// unwrapKey(format: KeyFormat, wrappedKey: BufferSource, unwrappingKey: CryptoKey, unwrapAlgorithm: AlgorithmIdentifier | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams, unwrappedKeyAlgorithm: AlgorithmIdentifier | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | AesKeyAlgorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
45+
// /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/verify) */
46+
// verify(algorithm: AlgorithmIdentifier | RsaPssParams | EcdsaParams, key: CryptoKey, signature: BufferSource, data: BufferSource): Promise<boolean>;
47+
// /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/wrapKey) */
48+
// wrapKey(format: KeyFormat, key: CryptoKey, wrappingKey: CryptoKey, wrapAlgorithm: AlgorithmIdentifier | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams): Promise<ArrayBuffer>;
49+
}
50+
51+
/**
52+
* Basic cryptography features available in the current context. It allows access to a cryptographically strong random number generator and to cryptographic primitives.
53+
*
54+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Crypto)
55+
*/
56+
interface Crypto {
57+
/**
58+
* Available only in secure contexts.
59+
*
60+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Crypto/subtle)
61+
*/
62+
readonly subtle: SubtleCrypto;
63+
/**
64+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Crypto/getRandomValues)
65+
*/
66+
getRandomValues<T extends ArrayBufferView | null>(array: T): T;
67+
/**
68+
* Available only in secure contexts.
69+
*
70+
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Crypto/randomUUID)
71+
*/
72+
randomUUID(): `${string}-${string}-${string}-${string}-${string}`;
73+
}
74+
75+
var Crypto: {
76+
prototype: Crypto;
77+
new(): Crypto;
78+
};
79+
80+
var crypto: Crypto;
81+
82+
}
83+
export { }

src/vs/base/common/hash.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,7 @@ function objectHash(obj: any, initialHashVal: number): number {
6969
}, initialHashVal);
7070
}
7171

72-
// this is shared global between browsers and nodejs
73-
declare const crypto: {
74-
subtle: {
75-
digest(a: string, b: ArrayBufferView): Promise<ArrayBuffer>;
76-
};
77-
};
72+
7873

7974
/** Hashes the input as SHA-1, returning a hex-encoded string. */
8075
export const hashAsync = (input: string | ArrayBufferView | VSBuffer) => {

src/vs/base/common/uuid.ts

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -10,72 +10,4 @@ export function isUUID(value: string): boolean {
1010
return _UUIDPattern.test(value);
1111
}
1212

13-
declare const crypto: undefined | {
14-
//https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#browser_compatibility
15-
getRandomValues?(data: Uint8Array): Uint8Array;
16-
//https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID#browser_compatibility
17-
randomUUID?(): string;
18-
};
19-
20-
export const generateUuid = (function (): () => string {
21-
22-
// use `randomUUID` if possible
23-
if (typeof crypto === 'object' && typeof crypto.randomUUID === 'function') {
24-
return crypto.randomUUID.bind(crypto);
25-
}
26-
27-
// use `randomValues` if possible
28-
let getRandomValues: (bucket: Uint8Array) => Uint8Array;
29-
if (typeof crypto === 'object' && typeof crypto.getRandomValues === 'function') {
30-
getRandomValues = crypto.getRandomValues.bind(crypto);
31-
32-
} else {
33-
getRandomValues = function (bucket: Uint8Array): Uint8Array {
34-
for (let i = 0; i < bucket.length; i++) {
35-
bucket[i] = Math.floor(Math.random() * 256);
36-
}
37-
return bucket;
38-
};
39-
}
40-
41-
// prep-work
42-
const _data = new Uint8Array(16);
43-
const _hex: string[] = [];
44-
for (let i = 0; i < 256; i++) {
45-
_hex.push(i.toString(16).padStart(2, '0'));
46-
}
47-
48-
return function generateUuid(): string {
49-
// get data
50-
getRandomValues(_data);
51-
52-
// set version bits
53-
_data[6] = (_data[6] & 0x0f) | 0x40;
54-
_data[8] = (_data[8] & 0x3f) | 0x80;
55-
56-
// print as string
57-
let i = 0;
58-
let result = '';
59-
result += _hex[_data[i++]];
60-
result += _hex[_data[i++]];
61-
result += _hex[_data[i++]];
62-
result += _hex[_data[i++]];
63-
result += '-';
64-
result += _hex[_data[i++]];
65-
result += _hex[_data[i++]];
66-
result += '-';
67-
result += _hex[_data[i++]];
68-
result += _hex[_data[i++]];
69-
result += '-';
70-
result += _hex[_data[i++]];
71-
result += _hex[_data[i++]];
72-
result += '-';
73-
result += _hex[_data[i++]];
74-
result += _hex[_data[i++]];
75-
result += _hex[_data[i++]];
76-
result += _hex[_data[i++]];
77-
result += _hex[_data[i++]];
78-
result += _hex[_data[i++]];
79-
return result;
80-
};
81-
})();
13+
export const generateUuid: () => string = crypto.randomUUID.bind(crypto);

0 commit comments

Comments
 (0)