deno.land / std@0.91.0 / node / crypto.ts

نووسراو ببینە
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
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.import { default as randomBytes } from "./_crypto/randomBytes.ts";import { createHash as stdCreateHash, Hasher, SupportedAlgorithm, supportedAlgorithms,} from "../hash/mod.ts";import { pbkdf2, pbkdf2Sync } from "./_crypto/pbkdf2.ts";import { Buffer } from "./buffer.ts";import { Transform } from "./stream.ts";import { TransformOptions } from "./_stream/transform.ts";import { encodeToString as encodeToHexString } from "../encoding/hex.ts";
/** * The Hash class is a utility for creating hash digests of data. It can be used in one of two ways: * * - As a stream that is both readable and writable, where data is written to produce a computed hash digest on the readable side, or * - Using the hash.update() and hash.digest() methods to produce the computed hash. * * The crypto.createHash() method is used to create Hash instances. Hash objects are not to be created directly using the new keyword. */export class Hash extends Transform { public hash: Hasher; constructor(algorithm: SupportedAlgorithm, _opts?: TransformOptions) { super({ transform(chunk: string, _encoding: string, callback: () => void): void { hash.update(chunk); callback(); }, flush(callback: () => void): void { // deno-lint-ignore no-this-before-super this.push(hash.digest()); callback(); }, }); const hash = this.hash = stdCreateHash(algorithm); }
// TODO(kt3k): Implement copy method // copy(options) { ... }
/** * Updates the hash content with the given data. */ update(data: string | ArrayBuffer, _encoding?: string): this { if (typeof data === "string") { data = new TextEncoder().encode(data); this.hash.update(data); } else { this.hash.update(data); } return this; }
/** * Calculates the digest of all of the data. * * If encoding is provided a string will be returned; otherwise a Buffer is returned. * * Supported encoding is currently 'hex' only. 'binary', 'base64' will be supported in the future versions. */ digest(encoding?: string): Buffer | string { const digest = this.hash.digest(); if (encoding === undefined) { return Buffer.from(digest); }
switch (encoding) { case "hex": { return encodeToHexString(new Uint8Array(digest)); } // TODO(kt3k): Support more output encodings such as base64, binary, etc default: { throw new Error( `The output encoding for hash digest is not impelemented: ${encoding}`, ); } } }}
/** * Creates and returns a Hash object that can be used to generate hash digests * using the given `algorithm`. Optional `options` argument controls stream behavior. */export function createHash( algorithm: SupportedAlgorithm, opts?: TransformOptions,) { return new Hash(algorithm, opts);}
/** * Returns an array of the names of the supported hash algorithms, such as 'sha1'. */export function getHashes(): SupportedAlgorithm[] { return supportedAlgorithms.slice();}
export default { Hash, createHash, getHashes, pbkdf2, pbkdf2Sync, randomBytes };export { pbkdf2, pbkdf2Sync, randomBytes };
std

Version Info

Tagged at
3 years ago

External Dependencies

No external dependencies 🎉