Skip to content

Commit

Permalink
Merge pull request #1 from TadeSF/browser-crypto-way-1
Browse files Browse the repository at this point in the history
Browser crypto way 1
  • Loading branch information
Strehk authored Oct 22, 2024
2 parents 80582c0 + ee7fe29 commit 753f85b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
16 changes: 13 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codenamize-ts",
"version": "0.0.6",
"version": "0.0.7",
"description": "Generate consistent easier-to-remember codenames from strings, numbers, or other seed inputs.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -35,7 +35,8 @@
"license": "MIT",
"private": false,
"dependencies": {
"big-integer": "^1.6.36"
"big-integer": "^1.6.36",
"jssha": "^3.3.1"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
Expand All @@ -47,4 +48,4 @@
"tsup": "^8.3.0",
"typescript": "^5.4.5"
}
}
}
32 changes: 26 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type BinaryLike, createHash } from "crypto";
import bigInt from "big-integer";
import jsSHA from "jssha";
import { adjectives, nouns } from "./particles";

interface ParticleMetadata {
Expand All @@ -16,7 +16,23 @@ interface Options {
maxItemChars?: number;
particles?: string[];
adjectiveCount?: number;
hashAlgorithm?: string;
hashAlgorithm?:
| "SHA-1"
| "SHA-224"
| "SHA-256"
| "SHA-384"
| "SHA-512"
| "SHA3-224"
| "SHA3-256"
| "SHA3-256"
| "SHA3-384"
| "SHA3-512"
| "SHAKE128"
| "SHAKE256"
| "CSHAKE128"
| "CSHAKE256"
| "KMAC128"
| "KMAC256";
separator?: string;
capitalize?: boolean;
_isParsed?: boolean;
Expand Down Expand Up @@ -107,7 +123,7 @@ export function parseOptions(options: Options | string | number): Options {
response.hashAlgorithm =
options && typeof options.hashAlgorithm === "string"
? options.hashAlgorithm
: "md5";
: "SHA-1";
response.separator =
options && typeof options.separator === "string" ? options.separator : "-";

Expand Down Expand Up @@ -153,9 +169,13 @@ export function getHash(options: Options) {
if (!useOptions.hashAlgorithm) {
throw new Error("Missing hash algorithm");
}
const hash = createHash(useOptions.hashAlgorithm);
hash.update(useOptions.seed as BinaryLike);
const hashDigest = hash.digest("hex");
const hash = new jsSHA(
// @ts-expect-error – The jsSHA library has incorrect types for the variant
options.hashAlgorithm ? options.hashAlgorithm : "SHA-1",
"TEXT",
);
hash.update(useOptions.seed ? useOptions.seed.toString() : "");
const hashDigest = hash.getHash("HEX");
return bigInt(hashDigest, 16).multiply("36413321723440003717");
}

Expand Down

0 comments on commit 753f85b

Please sign in to comment.