From ff63d5df89bd7d15019088fe5f988dd71ee5c4a9 Mon Sep 17 00:00:00 2001 From: Daniel Bachler Date: Wed, 7 Aug 2024 22:17:36 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20remove=20SHA-1=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../@ourworldindata/utils/src/Util.test.ts | 19 -------- packages/@ourworldindata/utils/src/Util.ts | 46 ------------------- packages/@ourworldindata/utils/src/index.ts | 5 -- 3 files changed, 70 deletions(-) diff --git a/packages/@ourworldindata/utils/src/Util.test.ts b/packages/@ourworldindata/utils/src/Util.test.ts index efc593dbfc0..c1cd463fac5 100755 --- a/packages/@ourworldindata/utils/src/Util.test.ts +++ b/packages/@ourworldindata/utils/src/Util.test.ts @@ -29,11 +29,6 @@ import { traverseEnrichedBlock, cartesian, formatInlineList, - getSHA1HashBytes, - bytesToBase64, - base64ToBytes, - bytesToHex, - hexToBytes, } from "./Util.js" import { BlockImageSize, @@ -800,17 +795,3 @@ describe(formatInlineList, () => { ) }) }) - -// a test to see if getSHA1HashBytes encoded to base64 and back to bytes is the same as the original bytes -describe("getSHA1HashBytes going back and forth through base64 and hex yields identical results", () => { - it("hashes a string and decodes it back to the same bytes", async () => { - const bytes = await getSHA1HashBytes("Hello World") - const base64 = bytesToBase64(bytes) - const decodedBytes = base64ToBytes(base64) - expect(decodedBytes).toEqual(bytes) - - const hex = bytesToHex(bytes) - const decodedBytes2 = hexToBytes(hex) - expect(decodedBytes2).toEqual(bytes) - }) -}) diff --git a/packages/@ourworldindata/utils/src/Util.ts b/packages/@ourworldindata/utils/src/Util.ts index 8a77db76ea6..553cb9f028d 100644 --- a/packages/@ourworldindata/utils/src/Util.ts +++ b/packages/@ourworldindata/utils/src/Util.ts @@ -189,22 +189,6 @@ import React from "react" import { match, P } from "ts-pattern" // import "crypto" -let subtleCrypto: any - -if (typeof globalThis.crypto !== "undefined" && globalThis.crypto.subtle) { - // Browsers and Cloudflare Workers - subtleCrypto = globalThis.crypto.subtle -} else { - // Node.js 18+ using the built-in webcrypto module - import("node:crypto") - .then(({ webcrypto }) => { - subtleCrypto = webcrypto.subtle - }) - .catch((err) => { - console.error("Failed to import crypto module:", err) - }) -} - export type NoUndefinedValues = { [P in keyof T]: Required> } @@ -480,25 +464,6 @@ export const cagr = ( ) } -export enum HashAlgorithm { - Sha1 = "sha1", -} - -export async function getHashBytes( - data: string, - algorithm: HashAlgorithm -): Promise { - const encoder = new TextEncoder() - const dataBuffer = encoder.encode(data) - const hashBuffer = await subtleCrypto.digest(algorithm, dataBuffer) - - return new Uint8Array(hashBuffer) -} - -export async function getSHA1HashBytes(data: string): Promise { - return getHashBytes(data, HashAlgorithm.Sha1) -} - export type Base64String = Nominal export type HexString = Nominal @@ -535,17 +500,6 @@ export function bytesToHex(bytes: Uint8Array): HexString { .join("") as HexString } -export async function getSHA1HashHex(data: string): Promise { - const hashBuffer = await getSHA1HashBytes(data) - return bytesToHex(hashBuffer) -} - -export async function getSHA1HashBase64(data: string): Promise { - const bytes = await getSHA1HashBytes(data) - const base64 = bytesToBase64(bytes) - return base64 -} - export const makeAnnotationsSlug = (columnSlug: string): string => `${columnSlug}-annotations` diff --git a/packages/@ourworldindata/utils/src/index.ts b/packages/@ourworldindata/utils/src/index.ts index aec1b648b1d..917a33f7b55 100644 --- a/packages/@ourworldindata/utils/src/index.ts +++ b/packages/@ourworldindata/utils/src/index.ts @@ -22,11 +22,6 @@ export { mapToObjectLiteral, type Base64String, type HexString, - type HashAlgorithm, - getSHA1HashBytes, - getSHA1HashHex, - getSHA1HashBase64, - getHashBytes, bytesToBase64, base64ToBytes, bytesToHex,