This file contains hidden or 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
export const createKeyPair = async () => { | |
let {publicKey , privateKey} = sodium.crypto_sign_keypair(); | |
const publicKey_base64 = sodium.to_base64(publicKey, base64_variants.ORIGINAL); | |
const privateKey_base64 = sodium.to_base64(privateKey, base64_variants.ORIGINAL); | |
return { publicKey : publicKey_base64, privateKey : privateKey_base64 }; |
This file contains hidden or 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
export const verifyHeader = async (headerParts: any, body: any, public_key: string) => { | |
const { signing_string } = await createSigningString(JSON.stringify(body), headerParts['created'], headerParts['expires']); | |
console.log("recreated signing string:"); | |
console.log(signing_string); | |
const verified = await verifyMessage(headerParts['signature'], signing_string, public_key); | |
return verified; | |
} | |
This file contains hidden or 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
export const verifyMessage = async (signedString: string, signingString: string, publicKey: string) => { | |
try { | |
await _sodium.ready; | |
const sodium = _sodium; | |
return sodium.crypto_sign_verify_detached(sodium.from_base64(signedString, base64_variants.ORIGINAL), signingString, sodium.from_base64(publicKey, base64_variants.ORIGINAL)); | |
} catch (error) { | |
return false | |
} | |
} |
This file contains hidden or 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
export const signMessage = async (signing_string: string, privateKey: string) => { | |
await _sodium.ready; | |
const sodium = _sodium; | |
const signedMessage = sodium.crypto_sign_detached(signing_string, sodium.from_base64(privateKey, base64_variants.ORIGINAL)); | |
return sodium.to_base64(signedMessage, base64_variants.ORIGINAL); |
This file contains hidden or 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
export const createSigningString = async (message: string, created?: string, expires?: string) => { | |
if (!created) created = Math.floor(new Date().getTime() / 1000).toString(); | |
if (!expires) expires = (parseInt(created) + (1 * 60 * 60)).toString(); | |
await _sodium.ready; | |
const sodium = _sodium; | |
const digest = sodium.crypto_generichash(64, sodium.from_string(message)); | |
const digest_base64 = sodium.to_base64(digest, base64_variants.ORIGINAL); | |
const signing_string = | |
`(created): ${created} | |
(expires): ${expires} |
This file contains hidden or 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
const signedMessage = sodium.crypto_sign_detached(signing_string, sodium.from_base64(privateKey, base64_variants.ORIGINAL)); | |
const signedMessage_ = sodium.to_base64(signedMessage, base64_variants.ORIGINAL); |
This file contains hidden or 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
const signing_string = `(created): ${created} | |
(expires): ${expires} | |
digest: BLAKE-512=${digest_base64}`; |
NewerOlder