Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into alex-noble-sync
Browse files Browse the repository at this point in the history
  • Loading branch information
ocavue committed Dec 14, 2023
2 parents 8bdfd7a + 6908b4e commit 60da4ae
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 @@ -8,6 +8,8 @@ import {
generateSalt,
hash,
hashPassword,
decryptBlobAsString,
encryptStringAsBlob,
} from '.'

import {arrayBufferToString} from './utils'
Expand Down Expand Up @@ -123,3 +125,15 @@ it('sanity checks v001 ciphertext', 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 @@ -50,6 +50,9 @@ export function 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 function encrypt({
key,
Expand All @@ -69,6 +72,24 @@ export function encrypt({
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 @@ -118,6 +139,26 @@ export function decrypt({
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 60da4ae

Please sign in to comment.