-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
198 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import argon2 from "argon2"; | ||
import crypto from "crypto"; // Node.js crypto for MD5 or SHA-1 | ||
|
||
export async function hashPassword( | ||
password: string, | ||
username: string | ||
): Promise<string> { | ||
// Hash the username to ensure it is the correct size for the salt | ||
const usernameHash = crypto | ||
.createHash("sha1") | ||
.update(username) | ||
.digest("base64"); | ||
|
||
// Use the username as the salt | ||
const salt = Buffer.from(usernameHash, "utf-8"); | ||
|
||
const passwordHash = await argon2.hash(password, { | ||
salt, | ||
type: argon2.argon2id, // Use Argon2id for balanced security | ||
timeCost: 3, | ||
memoryCost: 32768, // 32MB | ||
parallelism: 1, | ||
}); | ||
|
||
return passwordHash; | ||
} | ||
|
||
export async function generateKeyPairAndEncryptHash( | ||
username: string, | ||
password: string | ||
): Promise<{ publicKey: string; encryptedHash: string }> { | ||
// Generate the key pair | ||
const keyPair = await crypto.subtle.generateKey( | ||
{ | ||
name: "ECDSA", | ||
namedCurve: "P-256", // Use P-256 for lightweight elliptic curve | ||
}, | ||
true, | ||
["sign", "verify"] | ||
); | ||
|
||
// Export public key | ||
const publicKey = await crypto.subtle.exportKey("spki", keyPair.publicKey); | ||
|
||
// Hash the password with the username as the salt | ||
const passwordHash = await hashPassword(password, username); | ||
|
||
// Optionally hash the encodedHash with MD5 or SHA-1 | ||
const sha1Hash = crypto | ||
.createHash("sha1") | ||
.update(passwordHash) | ||
.digest("base64"); | ||
|
||
// Encrypt the SHA-1 hash with the private key | ||
const encodedHashBytes = new TextEncoder().encode(sha1Hash); | ||
const encryptedHash = await crypto.subtle.sign( | ||
{ | ||
name: "ECDSA", | ||
hash: { name: "SHA-256" }, | ||
}, | ||
keyPair.privateKey, | ||
encodedHashBytes | ||
); | ||
|
||
return { | ||
publicKey: Buffer.from(publicKey).toString("base64"), | ||
encryptedHash: Buffer.from(encryptedHash).toString("base64"), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters