Skip to content

Commit

Permalink
🔨 remove SHA-1 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Aug 7, 2024
1 parent 6b9b5b5 commit ff63d5d
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 70 deletions.
19 changes: 0 additions & 19 deletions packages/@ourworldindata/utils/src/Util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ import {
traverseEnrichedBlock,
cartesian,
formatInlineList,
getSHA1HashBytes,
bytesToBase64,
base64ToBytes,
bytesToHex,
hexToBytes,
} from "./Util.js"
import {
BlockImageSize,
Expand Down Expand Up @@ -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)
})
})
46 changes: 0 additions & 46 deletions packages/@ourworldindata/utils/src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = {
[P in keyof T]: Required<NonNullable<T[P]>>
}
Expand Down Expand Up @@ -480,25 +464,6 @@ export const cagr = (
)
}

export enum HashAlgorithm {
Sha1 = "sha1",
}

export async function getHashBytes(
data: string,
algorithm: HashAlgorithm
): Promise<Uint8Array> {
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<Uint8Array> {
return getHashBytes(data, HashAlgorithm.Sha1)
}

export type Base64String = Nominal<string, "Base64">
export type HexString = Nominal<string, "Hex">

Expand Down Expand Up @@ -535,17 +500,6 @@ export function bytesToHex(bytes: Uint8Array): HexString {
.join("") as HexString
}

export async function getSHA1HashHex(data: string): Promise<HexString> {
const hashBuffer = await getSHA1HashBytes(data)
return bytesToHex(hashBuffer)
}

export async function getSHA1HashBase64(data: string): Promise<Base64String> {
const bytes = await getSHA1HashBytes(data)
const base64 = bytesToBase64(bytes)
return base64
}

export const makeAnnotationsSlug = (columnSlug: string): string =>
`${columnSlug}-annotations`

Expand Down
5 changes: 0 additions & 5 deletions packages/@ourworldindata/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ export {
mapToObjectLiteral,
type Base64String,
type HexString,
type HashAlgorithm,
getSHA1HashBytes,
getSHA1HashHex,
getSHA1HashBase64,
getHashBytes,
bytesToBase64,
base64ToBytes,
bytesToHex,
Expand Down

0 comments on commit ff63d5d

Please sign in to comment.