Skip to content

Commit

Permalink
Added functions for encrypting/decrypting strings ↔ blobs (#11)
Browse files Browse the repository at this point in the history
* added functions for encrypting/decrypting strings ↔ blobs

* deprecating base64 encrypt
  • Loading branch information
vojto authored Aug 30, 2023
1 parent 9c7e454 commit e17ae84
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
generateSalt,
hash,
hashPassword,
decryptBlobAsString,
encryptStringAsBlob,
} from '.'

import {arrayBufferToString, stringToArrayBuffer} from './utils'
Expand Down Expand Up @@ -122,3 +124,15 @@ it('sanity checks v001 ciphertext', async function () {

expect(decrypted).toEqual(plaintext)
})

it('encrypts/decrypts blobs as strings', async function () {
const key = await generateEncryptionKey()

const plaintext = 'hello world'

const cipherblob = await encryptStringAsBlob({plaintext, key})

const decrypted = await decryptBlobAsString({cipherblob, key})

expect(decrypted).toEqual(plaintext)
})
41 changes: 41 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export const generateSalt = () => {
* @param key - In hex format
* @param plaintext
* @returns Base64 ciphertext string
*
* @deprecated This function converts cipherblob to base64 string, which is not
* efficient. Use one of the functions that return blob instead.
*/
export const encrypt = async ({
key,
Expand All @@ -70,6 +73,24 @@ export const encrypt = async ({
return [Defaults.Version, nonce, ciphertext].join(STRING_PARTITION)
}

/**
* Encrypt a plaintext string and output it as a Uint8Array blob.
* @param key - The encryption key in hex format.
* @param plaintext - The plaintext string to encrypt.
* @returns A Promise that resolves to the encrypted message as a Uint8Array.
*/
export const encryptStringAsBlob = async ({
key,
plaintext,
}: {
key: HexString
plaintext: Utf8String
}): Promise<EncryptedBlobMessage> => {
const plainblob = await stringToArrayBuffer(plaintext)

return encryptBlob({key, plainblob})
}

/**
* Encrypt a message (and associated data) with XChaCha20-Poly1305.
* @param key - In hex format
Expand Down Expand Up @@ -119,6 +140,26 @@ export const decrypt = async ({
return xChaChaDecrypt({key, ciphertext, nonce})
}

/**
* Decrypt an encrypted blob message and output it as a plaintext string.
* @param key - The decryption key in hex format.
* @param cipherblob - The encrypted message as a Uint8Array.
* @returns A Promise that resolves to the decrypted plaintext string or null if decryption fails.
*/
export const decryptBlobAsString = async ({
key,
cipherblob,
}: {
key: HexString
cipherblob: EncryptedBlobMessage
}): Promise<Utf8String | null> => {
const decryptedBlob = await decryptBlob({key, cipherblob})

if (!decryptedBlob) return null

return arrayBufferToString(decryptedBlob)
}

/**
* Decrypt a message (and associated data) with XChaCha20-Poly1305
* @param key - In hex format
Expand Down

0 comments on commit e17ae84

Please sign in to comment.