Skip to content

Commit

Permalink
refactor!: Remove unnecessary async methods in crypto package
Browse files Browse the repository at this point in the history
  • Loading branch information
tabcat committed Aug 16, 2023
1 parent 02b8932 commit dd77b85
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/crypto/src/aes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface AESCipher {
decrypt: (data: Uint8Array) => Promise<Uint8Array>
}

export async function create (key: Uint8Array, iv: Uint8Array): Promise<AESCipher> {
export function create (key: Uint8Array, iv: Uint8Array): AESCipher {
const mode = cipherMode(key)
const cipher = ciphers.createCipheriv(mode, key, iv)
const decipher = ciphers.createDecipheriv(mode, key, iv)
Expand Down
6 changes: 3 additions & 3 deletions packages/crypto/src/ciphers/aes-gcm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function create (opts?: CreateOptions): AESCipher {
const iterations = opts?.iterations ?? 32767
const algorithmTagLength = opts?.algorithmTagLength ?? 16

async function encryptWithKey (data: Uint8Array, key: Uint8Array): Promise<Uint8Array> {
function encryptWithKey (data: Uint8Array, key: Uint8Array): Uint8Array {
const nonce = crypto.randomBytes(nonceLength)

// Create the cipher instance.
Expand Down Expand Up @@ -43,7 +43,7 @@ export function create (opts?: CreateOptions): AESCipher {
const key = crypto.pbkdf2Sync(password, salt, iterations, keyLength, digest)

// Encrypt and prepend salt.
return uint8ArrayConcat([salt, await encryptWithKey(Uint8Array.from(data), key)])
return uint8ArrayConcat([salt, encryptWithKey(Uint8Array.from(data), key)])
}

/**
Expand All @@ -53,7 +53,7 @@ export function create (opts?: CreateOptions): AESCipher {
* this decryption cipher must be the same as those used to create
* the encryption cipher.
*/
async function decryptWithKey (ciphertextAndNonce: Uint8Array, key: Uint8Array): Promise<Uint8Array> {
function decryptWithKey (ciphertextAndNonce: Uint8Array, key: Uint8Array): Uint8Array {
// Create Uint8Arrays of nonce, ciphertext and tag.
const nonce = ciphertextAndNonce.subarray(0, nonceLength)
const ciphertext = ciphertextAndNonce.subarray(nonceLength, ciphertextAndNonce.length - algorithmTagLength)
Expand Down
8 changes: 4 additions & 4 deletions packages/crypto/test/aes/aes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('AES-CTR', () => {
const iv = new Uint8Array(16)
iv.fill(1)

const cipher = await crypto.aes.create(key, iv)
const cipher = crypto.aes.create(key, iv)

await encryptAndDecrypt(cipher)
await encryptAndDecrypt(cipher)
Expand All @@ -42,7 +42,7 @@ describe('AES-CTR', () => {
const iv = new Uint8Array(16)
iv.fill(1)

const cipher = await crypto.aes.create(key, iv)
const cipher = crypto.aes.create(key, iv)
// @ts-expect-error cannot index fixtures like this
const fixture = fixtures[length]

Expand Down Expand Up @@ -71,7 +71,7 @@ describe('AES-CTR', () => {
const iv = new Uint8Array(16)
iv.fill(1)

const cipher = await crypto.aes.create(key, iv)
const cipher = crypto.aes.create(key, iv)
// @ts-expect-error cannot index fixtures like this
const fixture = goFixtures[length]

Expand All @@ -90,7 +90,7 @@ describe('AES-CTR', () => {
it('checks key length', () => {
const key = new Uint8Array(5)
const iv = new Uint8Array(16)
return expect(crypto.aes.create(key, iv)).to.eventually.be.rejected.with.property('code', 'ERR_INVALID_KEY_LENGTH')
return expect(() => crypto.aes.create(key, iv)).to.throw().with.property('code', 'ERR_INVALID_KEY_LENGTH')
})
})

Expand Down

0 comments on commit dd77b85

Please sign in to comment.