2019-06-17 17:19:57 +00:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2022-08-11 11:51:20 +00:00
|
|
|
// https://github.com/golang/go/blob/master/LICENSE
|
2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-18 12:36:00 +00:00
|
|
|
// This module is browser compatible.
|
2022-08-11 11:51:20 +00:00
|
|
|
|
2023-12-13 02:57:59 +00:00
|
|
|
/**
|
|
|
|
* Port of the Go
|
2024-01-25 14:08:29 +00:00
|
|
|
* {@link https://github.com/golang/go/blob/go1.12.5/src/encoding/hex/hex.go | encoding/hex}
|
2022-08-11 11:51:20 +00:00
|
|
|
* library.
|
|
|
|
*
|
2022-11-25 11:40:23 +00:00
|
|
|
* ```ts
|
|
|
|
* import {
|
2023-09-18 01:50:08 +00:00
|
|
|
* decodeHex,
|
|
|
|
* encodeHex,
|
2024-04-29 02:57:30 +00:00
|
|
|
* } from "@std/encoding/hex";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* assertEquals(encodeHex("abc"), "616263");
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* assertEquals(
|
|
|
|
* decodeHex("616263"),
|
|
|
|
* new TextEncoder().encode("abc"),
|
|
|
|
* );
|
2022-11-25 11:40:23 +00:00
|
|
|
* ```
|
|
|
|
*
|
2022-08-11 11:51:20 +00:00
|
|
|
* @module
|
|
|
|
*/
|
2019-06-17 17:19:57 +00:00
|
|
|
|
2024-05-28 03:12:24 +00:00
|
|
|
import { validateBinaryLike } from "./_validate_binary_like.ts";
|
2024-01-25 14:08:29 +00:00
|
|
|
|
2020-12-10 19:45:45 +00:00
|
|
|
const hexTable = new TextEncoder().encode("0123456789abcdef");
|
2023-09-18 01:50:08 +00:00
|
|
|
const textEncoder = new TextEncoder();
|
|
|
|
const textDecoder = new TextDecoder();
|
2019-06-17 17:19:57 +00:00
|
|
|
|
2021-07-06 15:47:42 +00:00
|
|
|
function errInvalidByte(byte: number) {
|
|
|
|
return new TypeError(`Invalid byte '${String.fromCharCode(byte)}'`);
|
2019-06-17 17:19:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 15:47:42 +00:00
|
|
|
function errLength() {
|
|
|
|
return new RangeError("Odd length hex string");
|
2019-06-17 17:19:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 15:47:42 +00:00
|
|
|
/** Converts a hex character into its value. */
|
2020-07-09 20:50:19 +00:00
|
|
|
function fromHexChar(byte: number): number {
|
|
|
|
// '0' <= byte && byte <= '9'
|
|
|
|
if (48 <= byte && byte <= 57) return byte - 48;
|
|
|
|
// 'a' <= byte && byte <= 'f'
|
|
|
|
if (97 <= byte && byte <= 102) return byte - 97 + 10;
|
|
|
|
// 'A' <= byte && byte <= 'F'
|
|
|
|
if (65 <= byte && byte <= 70) return byte - 65 + 10;
|
|
|
|
|
|
|
|
throw errInvalidByte(byte);
|
2019-06-17 17:19:57 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 02:57:59 +00:00
|
|
|
/**
|
|
|
|
* Converts data into a hex-encoded string.
|
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* @param src The data to encode.
|
|
|
|
*
|
|
|
|
* @returns The hex-encoded string.
|
|
|
|
*
|
|
|
|
* @example Usage
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { encodeHex } from "@std/encoding/hex";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* assertEquals(encodeHex("abc"), "616263");
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2023-09-18 01:50:08 +00:00
|
|
|
export function encodeHex(src: string | Uint8Array | ArrayBuffer): string {
|
2023-09-22 09:47:24 +00:00
|
|
|
const u8 = validateBinaryLike(src);
|
2023-09-18 01:50:08 +00:00
|
|
|
|
|
|
|
const dst = new Uint8Array(u8.length * 2);
|
2024-07-08 01:36:00 +00:00
|
|
|
for (let i = 0; i < u8.length; i++) {
|
2024-02-24 20:24:08 +00:00
|
|
|
const v = u8[i]!;
|
|
|
|
dst[i * 2] = hexTable[v >> 4]!;
|
|
|
|
dst[i * 2 + 1] = hexTable[v & 0x0f]!;
|
2023-09-18 01:50:08 +00:00
|
|
|
}
|
|
|
|
return textDecoder.decode(dst);
|
|
|
|
}
|
|
|
|
|
2023-12-13 02:57:59 +00:00
|
|
|
/**
|
|
|
|
* Decodes the given hex-encoded string. If the input is malformed, an error is
|
|
|
|
* thrown.
|
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* @param src The hex-encoded string to decode.
|
|
|
|
*
|
|
|
|
* @returns The decoded data.
|
|
|
|
*
|
|
|
|
* @example Usage
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { decodeHex } from "@std/encoding/hex";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* assertEquals(
|
|
|
|
* decodeHex("616263"),
|
|
|
|
* new TextEncoder().encode("abc"),
|
|
|
|
* );
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2023-09-18 01:50:08 +00:00
|
|
|
export function decodeHex(src: string): Uint8Array {
|
|
|
|
const u8 = textEncoder.encode(src);
|
|
|
|
const dst = new Uint8Array(u8.length / 2);
|
|
|
|
for (let i = 0; i < dst.length; i++) {
|
2024-02-24 20:24:08 +00:00
|
|
|
const a = fromHexChar(u8[i * 2]!);
|
|
|
|
const b = fromHexChar(u8[i * 2 + 1]!);
|
2023-09-18 01:50:08 +00:00
|
|
|
dst[i] = (a << 4) | b;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u8.length % 2 === 1) {
|
|
|
|
// Check for invalid char before reporting bad length,
|
|
|
|
// since the invalid char (if present) is an earlier problem.
|
2024-02-24 20:24:08 +00:00
|
|
|
fromHexChar(u8[dst.length * 2]!);
|
2023-09-18 01:50:08 +00:00
|
|
|
throw errLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|