@httpland/range-request-middleware
Version:
HTTP range request middleware
34 lines (33 loc) • 1.18 kB
JavaScript
;
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
Object.defineProperty(exports, "__esModule", { value: true });
exports.toHashString = void 0;
const hex_js_1 = require("../encoding/hex.js");
const base64_js_1 = require("../encoding/base64.js");
const decoder = new TextDecoder();
/**
* Converts a hash to a string with a given encoding.
* @example
* ```ts
* import { crypto } from "https://deno.land/std@$STD_VERSION/crypto/crypto.ts";
* import { toHashString } from "https://deno.land/std@$STD_VERSION/crypto/to_hash_string.ts"
*
* const hash = await crypto.subtle.digest("SHA-384", new TextEncoder().encode("You hear that Mr. Anderson?"));
*
* // Hex encoding by default
* console.log(toHashString(hash));
*
* // Or with base64 encoding
* console.log(toHashString(hash, "base64"));
* ```
*/
function toHashString(hash, encoding = "hex") {
switch (encoding) {
case "hex":
return decoder.decode((0, hex_js_1.encode)(new Uint8Array(hash)));
case "base64":
return (0, base64_js_1.encode)(hash);
}
}
exports.toHashString = toHashString;