forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.ts
More file actions
28 lines (25 loc) · 875 Bytes
/
crypto.ts
File metadata and controls
28 lines (25 loc) · 875 Bytes
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
// tslint:disable: no-any
import { createHash } from 'crypto';
import { injectable } from 'inversify';
import { traceError } from './logger';
import { ICryptoUtils, IHashFormat } from './types';
/**
* Implements tools related to cryptography
*/
@injectable()
export class CryptoUtils implements ICryptoUtils {
public createHash<E extends keyof IHashFormat>(data: string, hashFormat: E): IHashFormat[E] {
const hash = createHash('sha512').update(data).digest('hex');
if (hashFormat === 'number') {
const result = parseInt(hash, 16);
if (isNaN(result)) {
traceError(`Number hash for data '${data}' is NaN`);
}
return result as any;
}
return hash as any;
}
}