1616 * Copyright (c) 2010-2014 Digital Bazaar, Inc.
1717 */
1818
19- import type { Algorithm } from './cipher'
19+ import type { Algorithm , BlockCipher } from './cipher'
2020import type { CipherMode , CipherModeOptions } from './cipher-modes'
21- import { registerAlgorithm as registerCipherAlgorithm } from './cipher'
21+ import { createCipher , registerAlgorithm as registerCipherAlgorithm } from './cipher'
2222import { modes } from './cipher-modes'
2323import { 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 */
4636let init = false // not yet initialized
4737const 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+ }
0 commit comments