Skip to content

Commit

Permalink
refactor: improve type definitions for better type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoDePatta committed Sep 30, 2024
1 parent 618db74 commit 55c1265
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
18 changes: 16 additions & 2 deletions components/utils/hash-generator.utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import crypto, { BinaryToTextEncoding } from "crypto";

export type Algorithm =
| "sha256"
| "sha512"
| "md5"
| "pbkdf2"
| "hmac-sha256"
| "hmac-sha512";

export const generateHash = (
algorithm: string,
algorithm: Algorithm,
data: string,
encoding: BinaryToTextEncoding = "hex",
secretKey?: string
Expand All @@ -10,9 +18,15 @@ export const generateHash = (
throw new Error("Data must be a non-empty string");
}

if (algorithm.startsWith("hmac") && !secretKey) {
throw new Error("Secret key must be provided for HMAC algorighms");
}

const secret = secretKey || crypto.randomBytes(32).toString("hex");

try {
const hash = algorithm.startsWith("hmac")
? crypto.createHmac(algorithm.replace("hmac-", ""), secretKey!)
? crypto.createHmac(algorithm.replace("hmac-", ""), secret)
: crypto.createHash(algorithm);

return hash.update(data).digest(encoding);
Expand Down
38 changes: 18 additions & 20 deletions pages/utilities/hash-generator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,24 @@ import Meta from "@/components/Meta";
import { CMDK } from "@/components/CMDK";
import { Combobox } from "@/components/ds/ComboboxComponent";
import { Input } from "@/components/ds/InputComponent";
import crypto, { BinaryToTextEncoding } from "crypto";
import { generateHash } from "@/components/utils/hash-generator.utils";
import crypto, { BinaryToTextEncoding, Encoding } from "crypto";
import {
Algorithm,
generateHash,
} from "@/components/utils/hash-generator.utils";
import GitHubContribution from "@/components/GitHubContribution";

const MAX_ITERATIONS = 50_000;

type Algorithm =
| "sha256"
| "sha512"
| "md5"
| "pbkdf2"
| "hmac-sha256"
| "hmac-sha512";

interface Option {
value: string;
type HashFunctions = {
[K in Algorithm]: () => string;
};
interface Option<T extends string> {
value: T;
label: string;
}

const algorithmOptions: Option[] = [
const algorithmOptions: Option<Algorithm>[] = [
{ value: "sha256", label: "SHA-256" },
{ value: "sha512", label: "SHA-512" },
{ value: "md5", label: "MD5" },
Expand All @@ -39,7 +37,7 @@ const algorithmOptions: Option[] = [
{ value: "hmac-sha512", label: "HMAC-SHA-512" },
];

const encodingOptions: Option[] = [
const encodingOptions: Option<Encoding>[] = [
{ value: "hex", label: "Hex" },
{ value: "base64", label: "Base64" },
{ value: "latin1", label: "Latin1" },
Expand Down Expand Up @@ -77,8 +75,8 @@ export default function HashGenerator() {
[]
);

const handleAlgorithmChange = useCallback((value: string) => {
setAlgorithm(value as Algorithm);
const handleAlgorithmChange = useCallback((value: Algorithm) => {
setAlgorithm(value);
}, []);

const handleIterationsChange = useCallback(
Expand All @@ -96,11 +94,11 @@ export default function HashGenerator() {
[]
);

const handleEncodingChange = useCallback((value: string) => {
setEncoding(value as BinaryToTextEncoding);
const handleEncodingChange = useCallback((value: BinaryToTextEncoding) => {
setEncoding(value);
}, []);

const hashFunctions = useMemo(() => {
const hashFunctions = useMemo((): HashFunctions => {
const resolvedSecretKey =
secretKey || crypto.randomBytes(32).toString("hex");

Expand All @@ -112,7 +110,7 @@ export default function HashGenerator() {
const salt = saltInput || crypto.randomBytes(16).toString("hex");
return crypto
.pbkdf2Sync(textInput, salt, iterations, outputLength, "sha512")
.toString(encoding as BufferEncoding);
.toString(encoding);
},
"hmac-sha256": () =>
generateHash("hmac-sha256", textInput, encoding, resolvedSecretKey),
Expand Down

0 comments on commit 55c1265

Please sign in to comment.