Skip to content

Commit

Permalink
refactor!: remove unnecessary async from crypto methods (#1963)
Browse files Browse the repository at this point in the history
Removed async keyword from function which don't:

1) do any async work
2) return a promise
3) have an async browser equivalent

Closes: #1949

---------

Co-authored-by: Chad Nehemiah <chad.nehemiah94@gmail.com>
Co-authored-by: achingbrain <alex@achingbrain.net>
  • Loading branch information
3 people authored Nov 27, 2023
1 parent 4a474d5 commit e2267d4
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 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 @@ -51,7 +51,7 @@ export interface AESCipher {
* @param key - The key, if length `16` then `AES 128` is used. For length `32`, `AES 256` is used
* @param iv - Must have length `16`
*/
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/src/keys/ed25519-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const KEYS_BYTE_LENGTH = 32
export { PUBLIC_KEY_BYTE_LENGTH as publicKeyLength }
export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength }

export async function generateKey (): Promise<Uint8ArrayKeyPair> {
export function generateKey (): Uint8ArrayKeyPair {
// the actual private key (32 bytes)
const privateKeyRaw = ed.utils.randomPrivateKey()
const publicKey = ed.getPublicKey(privateKeyRaw)
Expand All @@ -26,7 +26,7 @@ export async function generateKey (): Promise<Uint8ArrayKeyPair> {
/**
* Generate keypair from a 32 byte uint8array
*/
export async function generateKeyFromSeed (seed: Uint8Array): Promise<Uint8ArrayKeyPair> {
export function generateKeyFromSeed (seed: Uint8Array): Uint8ArrayKeyPair {
if (seed.length !== KEYS_BYTE_LENGTH) {
throw new TypeError('"seed" must be 32 bytes in length.')
} else if (!(seed instanceof Uint8Array)) {
Expand All @@ -45,13 +45,13 @@ export async function generateKeyFromSeed (seed: Uint8Array): Promise<Uint8Array
}
}

export async function hashAndSign (privateKey: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<Uint8Array> {
export function hashAndSign (privateKey: Uint8Array, msg: Uint8Array | Uint8ArrayList): Uint8Array {
const privateKeyRaw = privateKey.subarray(0, KEYS_BYTE_LENGTH)

return ed.sign(msg instanceof Uint8Array ? msg : msg.subarray(), privateKeyRaw)
}

export async function hashAndVerify (publicKey: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<boolean> {
export function hashAndVerify (publicKey: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): boolean {
return ed.verify(sig, msg instanceof Uint8Array ? msg : msg.subarray(), publicKey)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/crypto/src/keys/ed25519-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ export function unmarshalEd25519PublicKey (bytes: Uint8Array): Ed25519PublicKey
}

export async function generateKeyPair (): Promise<Ed25519PrivateKey> {
const { privateKey, publicKey } = await crypto.generateKey()
const { privateKey, publicKey } = crypto.generateKey()
return new Ed25519PrivateKey(privateKey, publicKey)
}

export async function generateKeyPairFromSeed (seed: Uint8Array): Promise<Ed25519PrivateKey> {
const { privateKey, publicKey } = await crypto.generateKeyFromSeed(seed)
const { privateKey, publicKey } = crypto.generateKeyFromSeed(seed)
return new Ed25519PrivateKey(privateKey, publicKey)
}

Expand Down
13 changes: 6 additions & 7 deletions packages/crypto/src/keys/ed25519.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import crypto from 'crypto'
import { promisify } from 'util'
import { concat as uint8arrayConcat } from 'uint8arrays/concat'
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
import { toString as uint8arrayToString } from 'uint8arrays/to-string'
import type { Uint8ArrayKeyPair } from './interface.js'
import type { Uint8ArrayList } from 'uint8arraylist'

const keypair = promisify(crypto.generateKeyPair)
const keypair = crypto.generateKeyPairSync

const PUBLIC_KEY_BYTE_LENGTH = 32
const PRIVATE_KEY_BYTE_LENGTH = 64 // private key is actually 32 bytes but for historical reasons we concat private and public keys
Expand Down Expand Up @@ -37,8 +36,8 @@ function derivePublicKey (privateKey: Uint8Array): Uint8Array {
return uint8arrayFromString(jwk.x, 'base64url')
}

export async function generateKey (): Promise<Uint8ArrayKeyPair> {
const key = await keypair('ed25519', {
export function generateKey (): Uint8ArrayKeyPair {
const key = keypair('ed25519', {
publicKeyEncoding: { type: 'spki', format: 'jwk' },
privateKeyEncoding: { type: 'pkcs8', format: 'jwk' }
})
Expand All @@ -57,7 +56,7 @@ export async function generateKey (): Promise<Uint8ArrayKeyPair> {
/**
* Generate keypair from a 32 byte uint8array
*/
export async function generateKeyFromSeed (seed: Uint8Array): Promise<Uint8ArrayKeyPair> {
export function generateKeyFromSeed (seed: Uint8Array): Uint8ArrayKeyPair {
if (seed.length !== KEYS_BYTE_LENGTH) {
throw new TypeError('"seed" must be 32 bytes in length.')
} else if (!(seed instanceof Uint8Array)) {
Expand All @@ -73,7 +72,7 @@ export async function generateKeyFromSeed (seed: Uint8Array): Promise<Uint8Array
}
}

export async function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<Buffer> {
export function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Buffer {
if (!(key instanceof Uint8Array)) {
throw new TypeError('"key" must be a node.js Buffer, or Uint8Array.')
}
Expand Down Expand Up @@ -104,7 +103,7 @@ export async function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8Array
return crypto.sign(null, msg instanceof Uint8Array ? msg : msg.subarray(), obj)
}

export async function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<boolean> {
export function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): boolean {
if (key.byteLength !== PUBLIC_KEY_BYTE_LENGTH) {
throw new TypeError('"key" must be 32 bytes in length.')
} else if (!(key instanceof Uint8Array)) {
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 e2267d4

Please sign in to comment.