-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use webcrypto/aes-cbc-256, remove blockstack.js dep, closes #176
- Loading branch information
1 parent
61042de
commit 68ae719
Showing
8 changed files
with
133 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Buffer } from 'buffer'; | ||
|
||
const algorithmName = 'AES-CBC'; | ||
|
||
function extractEncryptionKey(hash: Uint8Array) { | ||
return hash.slice(0, 32); | ||
} | ||
|
||
function extractEncryptionInitVector(hash: Uint8Array) { | ||
return hash.slice(32, hash.length); | ||
} | ||
|
||
async function deriveWebCryptoKey(derivedKeyHash: Uint8Array) { | ||
const format = 'raw'; | ||
const key = extractEncryptionKey(derivedKeyHash); | ||
const algorithm = { name: algorithmName }; | ||
const extractable = false; | ||
const keyUsages: KeyUsage[] = ['encrypt', 'decrypt']; | ||
return crypto.subtle.importKey(format, key, algorithm, extractable, keyUsages); | ||
} | ||
|
||
interface EncryptMnemonicArgs { | ||
mnemonic: string; | ||
derivedKeyHash: Uint8Array; | ||
} | ||
|
||
export async function encryptMnemonic({ mnemonic, derivedKeyHash }: EncryptMnemonicArgs) { | ||
const key = await deriveWebCryptoKey(derivedKeyHash); | ||
const iv = extractEncryptionInitVector(derivedKeyHash); | ||
const cipherArrayBuffer = await crypto.subtle.encrypt( | ||
{ name: algorithmName, iv }, | ||
key, | ||
new TextEncoder().encode(mnemonic) | ||
); | ||
return Buffer.from(cipherArrayBuffer).toString('hex'); | ||
} | ||
|
||
interface DecryptMnemonicArgs { | ||
encryptedMnemonic: string; | ||
derivedKeyHash: Uint8Array; | ||
} | ||
|
||
export async function decryptMnemonic({ encryptedMnemonic, derivedKeyHash }: DecryptMnemonicArgs) { | ||
if (derivedKeyHash.length !== 48) throw new Error('Key must be of length 48'); | ||
const key = await deriveWebCryptoKey(derivedKeyHash); | ||
const iv = extractEncryptionInitVector(derivedKeyHash); | ||
const algorithm = { name: 'AES-CBC', iv }; | ||
const encryptedBuffer = new Buffer(encryptedMnemonic, 'hex'); | ||
const decrypted = await crypto.subtle.decrypt(algorithm, key, encryptedBuffer); | ||
const textDecoder = new TextDecoder(); | ||
return textDecoder.decode(decrypted); | ||
} |
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
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