Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(crypto): complete documentation #3921

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions crypto/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ import {
} from "./_wasm/mod.ts";
import { fnv } from "./_fnv/mod.ts";

export { type WasmDigestAlgorithm, wasmDigestAlgorithms };

/**
* A copy of the global WebCrypto interface, with methods bound so they're
* safe to re-export.
Expand Down Expand Up @@ -186,6 +188,7 @@ export interface StdSubtleCrypto extends SubtleCrypto {

/** Extensions to the Web {@linkcode Crypto} interface. */
export interface StdCrypto extends Crypto {
/** Extension to the {@linkcode crypto.SubtleCrypto} interface. */
readonly subtle: StdSubtleCrypto;
}

Expand Down Expand Up @@ -213,7 +216,7 @@ const stdCrypto: StdCrypto = ((x) => x)({

const bytes = bufferSourceBytes(data);

if (FNVAlgorithms.includes(name)) {
if (FNV_ALGORITHMS.includes(name)) {
return fnv(name, bytes);
}

Expand Down Expand Up @@ -277,7 +280,7 @@ const stdCrypto: StdCrypto = ((x) => x)({

const bytes = bufferSourceBytes(data);

if (FNVAlgorithms.includes(name)) {
if (FNV_ALGORITHMS.includes(name)) {
return fnv(name, bytes);
}

Expand All @@ -304,7 +307,7 @@ const stdCrypto: StdCrypto = ((x) => x)({
},
});

const FNVAlgorithms = ["FNV32", "FNV32A", "FNV64", "FNV64A"];
const FNV_ALGORITHMS = ["FNV32", "FNV32A", "FNV64", "FNV64A"];

/** Digest algorithms supported by WebCrypto. */
const webCryptoDigestAlgorithms = [
Expand All @@ -315,7 +318,10 @@ const webCryptoDigestAlgorithms = [
"SHA-1",
] as const;

/** FNV (Fowler/Noll/Vo) algorithms names. */
export type FNVAlgorithms = "FNV32" | "FNV32A" | "FNV64" | "FNV64A";

/** Extended digest algorithm names. */
export type DigestAlgorithmName = WasmDigestAlgorithm | FNVAlgorithms;

/*
Expand All @@ -341,11 +347,15 @@ function assertValidDigestLength(value?: number) {
}
}

/** Extended digest algorithm objects. */
export type DigestAlgorithmObject = {
name: DigestAlgorithmName;
length?: number;
};

/**
* Extended digest algorithms accepted by {@linkcode stdCrypto.subtle.digest}.
*/
export type DigestAlgorithm = DigestAlgorithmName | DigestAlgorithmObject;

function normalizeAlgorithm(algorithm: DigestAlgorithm) {
Expand Down
4 changes: 2 additions & 2 deletions crypto/to_hash_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { encodeHex } from "../encoding/hex.ts";
import { encodeBase64 } from "../encoding/base64.ts";

/**
* @deprecated (will be removed after 0.209.0) Use {@linkcode encodeHex} or {@linkcode encodeBase64} instead.
*
* Converts a hash to a string with a given encoding.
* @example
* ```ts
Expand All @@ -21,6 +19,8 @@ import { encodeBase64 } from "../encoding/base64.ts";
* // Or with base64 encoding
* console.log(toHashString(hash, "base64"));
* ```
*
* @deprecated (will be removed after 0.209.0) Use {@linkcode encodeHex} or {@linkcode encodeBase64} instead.
*/
export function toHashString(
hash: ArrayBuffer,
Expand Down
7 changes: 5 additions & 2 deletions crypto/unstable_keystack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class KeyStack {
return this.#cryptoKeys.get(key)!;
}

/** Number of keys */
get length(): number {
return this.#keys.length;
}
Expand Down Expand Up @@ -145,7 +146,9 @@ export class KeyStack {
return -1;
}

[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string) {
[Symbol.for("Deno.customInspect")](
inspect: (value: unknown) => string,
): string {
const { length } = this;
return `${this.constructor.name} ${inspect({ length })}`;
}
Expand All @@ -155,7 +158,7 @@ export class KeyStack {
// deno-lint-ignore no-explicit-any
options: any,
inspect: (value: unknown, options?: unknown) => string,
) {
): string {
if (depth < 0) {
return options.stylize(`[${this.constructor.name}]`, "special");
}
Expand Down