Skip to content

Commit ebf7074

Browse files
committed
chore: wip
1 parent 3c9d7dc commit ebf7074

5 files changed

Lines changed: 29 additions & 16 deletions

File tree

src/aes.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
* Copyright (c) 2010-2014 Digital Bazaar, Inc.
1717
*/
1818

19-
import type { Algorithm } from './cipher'
19+
import type { Algorithm, BlockCipher } from './cipher'
2020
import type { CipherMode, CipherModeOptions } from './cipher-modes'
21-
import { registerAlgorithm as registerCipherAlgorithm } from './cipher'
21+
import { createCipher, registerAlgorithm as registerCipherAlgorithm } from './cipher'
2222
import { modes } from './cipher-modes'
2323
import { ByteStringBuffer, createBuffer } from './utils'
2424

@@ -32,16 +32,6 @@ interface AlgorithmOptions {
3232
decrypt?: boolean
3333
}
3434

35-
interface AlgorithmFactory {
36-
(): {
37-
mode: {
38-
start: (options: Partial<CipherModeOptions>) => void
39-
encrypt: (input: ByteStringBuffer, output: ByteStringBuffer, finish: boolean) => boolean
40-
decrypt: (input: ByteStringBuffer, output: ByteStringBuffer, finish: boolean) => boolean
41-
}
42-
}
43-
}
44-
4535
/** AES implementation */
4636
let init = false // not yet initialized
4737
const Nb = 4 // number of words comprising the state (AES = 4)
@@ -159,7 +149,7 @@ export class AESAlgorithm implements Algorithm {
159149
*
160150
* @return the expanded key.
161151
*/
162-
function expandKey(key: number[], decrypt: boolean) {
152+
export function expandKey(key: number[], decrypt: boolean): number[] {
163153
if (!init) {
164154
initialize()
165155
}
@@ -168,10 +158,11 @@ function expandKey(key: number[], decrypt: boolean) {
168158

169159
/** Register AES algorithms */
170160

171-
function registerAESAlgorithm(name: string, mode: any) {
161+
export function registerAESAlgorithm(name: string, mode: any): void {
172162
const factory = function () {
173163
return new AESAlgorithm(name, mode)
174164
}
165+
175166
registerCipherAlgorithm(name, factory)
176167
}
177168

@@ -849,3 +840,17 @@ export function _updateBlock(w: number[], input: number[], output: number[], dec
849840
^ (sub[b >>> 8 & 255] << 8)
850841
^ (sub[c & 255]) ^ w[++i]
851842
}
843+
844+
function createEncryptionCipher(key: string, bits: string | Buffer): BlockCipher {
845+
return createCipher(key, bits)
846+
}
847+
848+
function createDecryptionCipher(key: string, bits: string | Buffer): BlockCipher {
849+
return createCipher(key, bits)
850+
}
851+
852+
export const aes = {
853+
createEncryptionCipher,
854+
createDecryptionCipher,
855+
registerAESAlgorithm,
856+
}

src/md5.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function create(): MessageDigest {
5252
let _input = createBuffer()
5353

5454
// used for word storage
55-
const _w: number[] = Array.from({ length: 16 }).fill(0)
55+
const _w: number[] = new Array(16).fill(0)
5656

5757
// message digest object
5858
const md: MessageDigest = {

src/random.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ interface ExtendedNavigator extends Navigator {
5656

5757
declare global {
5858
interface Window {
59+
// @ts-expect-error unsure if there is a better way to do this
5960
crypto: Crypto
61+
// @ts-expect-error unsure if there is a better way to do this
6062
msCrypto?: Crypto
63+
// @ts-expect-error unsure if there is a better way to do this
6164
navigator: ExtendedNavigator
65+
// @ts-expect-error unsure if there is a better way to do this
6266
document?: any
6367
}
6468
}

src/sha1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function create(): MessageDigest {
4444
let _input = createBuffer()
4545

4646
// Fix array initialization with proper typing
47-
const _w = Array.from({ length: 80 }).fill(0)
47+
const _w: number[] = new Array(80).fill(0)
4848

4949
// message digest object
5050
const md: MessageDigest = {

src/sha512.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,10 @@ const createSHA512_256: () => MessageDigest = () => create('SHA-512/256')
571571
// Create SHA-512/224 implementation
572572
const createSHA512_224: () => MessageDigest = () => create('SHA-512/224')
573573

574+
export const sha384: MessageDigest = createSHA384()
575+
export const sha512_256: MessageDigest = createSHA512_256()
576+
export const sha512_224: MessageDigest = createSHA512_224()
577+
574578
// Export all implementations
575579
export const sha512: SHA512API = {
576580
create,

0 commit comments

Comments
 (0)