Skip to content

Commit

Permalink
Remove WordArray functionality from BufferUtils
Browse files Browse the repository at this point in the history
No longer needed as of 90ea5e8.

TODO look at the remaining mentions of WordArray
  • Loading branch information
lawrence-forooghian committed May 24, 2023
1 parent 90ea5e8 commit 2357cc0
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 55 deletions.
3 changes: 1 addition & 2 deletions src/common/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type Bufferlike = WebBufferUtils.Bufferlike | NodeBufferUtils.Bufferlike;
type BufferUtilsOutput = WebBufferUtils.Output | NodeBufferUtils.Output;
type ToBufferOutput = WebBufferUtils.ToBufferOutput | NodeBufferUtils.ToBufferOutput;
type ComparableBuffer = WebBufferUtils.ComparableBuffer | NodeBufferUtils.ComparableBuffer;
type WordArrayLike = WebBufferUtils.WordArrayLike | NodeBufferUtils.WordArrayLike;

export default class Platform {
static Config: IPlatformConfig;
Expand All @@ -23,7 +22,7 @@ export default class Platform {
BufferUtils object that accepts a broader range of data types than it
can in reality handle.
*/
static BufferUtils: IBufferUtils<Bufferlike, BufferUtilsOutput, ToBufferOutput, ComparableBuffer, WordArrayLike>;
static BufferUtils: IBufferUtils<Bufferlike, BufferUtilsOutput, ToBufferOutput, ComparableBuffer>;
/*
This should be an object that implements the ICrypto interface, but (for the
same reasons as described in the BufferUtils comment above) Platform doesn’t
Expand Down
6 changes: 2 additions & 4 deletions src/common/types/IBufferUtils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { TypedArray } from './IPlatformConfig';

export default interface IBufferUtils<Bufferlike, Output, ToBufferOutput, ComparableBuffer, WordArrayLike> {
export default interface IBufferUtils<Bufferlike, Output, ToBufferOutput, ComparableBuffer> {
base64CharSet: string;
hexCharSet: string;
isBuffer: (buffer: unknown) => buffer is Bufferlike;
isWordArray: (val: unknown) => val is WordArrayLike;
// On browser this returns a Uint8Array, on node a Buffer
toBuffer: (buffer: Bufferlike) => ToBufferOutput;
toArrayBuffer: (buffer: Bufferlike | WordArrayLike) => ArrayBuffer;
toArrayBuffer: (buffer: Bufferlike) => ArrayBuffer;
base64Encode: (buffer: Bufferlike) => string;
base64Decode: (string: string) => Output;
hexEncode: (buffer: Bufferlike) => string;
Expand All @@ -17,5 +16,4 @@ export default interface IBufferUtils<Bufferlike, Output, ToBufferOutput, Compar
bufferCompare: (buffer1: ComparableBuffer, buffer2: ComparableBuffer) => number;
byteLength: (buffer: Bufferlike) => number;
typedArrayToBuffer: (typedArray: TypedArray) => Bufferlike;
toWordArray: (buffer: Bufferlike | number[]) => WordArrayLike;
}
5 changes: 0 additions & 5 deletions src/common/types/crypto-js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ declare module 'crypto-js/build/enc-utf8' {
export const stringify: typeof CryptoJS.enc.Utf8.stringify;
}

declare module 'crypto-js/build/lib-typedarrays' {
import CryptoJS from 'crypto-js';
export default CryptoJS.lib.WordArray;
}

declare module 'crypto-js/build/hmac-sha256' {
import CryptoJS from 'crypto-js';
export default CryptoJS.HmacSHA256;
Expand Down
15 changes: 2 additions & 13 deletions src/platform/nodejs/lib/util/bufferutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ export type Bufferlike = Buffer | ArrayBuffer | TypedArray;
export type Output = Buffer;
export type ToBufferOutput = Buffer;
export type ComparableBuffer = Buffer;
export type WordArrayLike = never;

class BufferUtils implements IBufferUtils<Bufferlike, Output, ToBufferOutput, ComparableBuffer, WordArrayLike> {
class BufferUtils implements IBufferUtils<Bufferlike, Output, ToBufferOutput, ComparableBuffer> {
base64CharSet: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
hexCharSet: string = '0123456789abcdef';

Expand Down Expand Up @@ -47,7 +46,7 @@ class BufferUtils implements IBufferUtils<Bufferlike, Output, ToBufferOutput, Co
return Buffer.isBuffer(buffer) || this.isArrayBuffer(buffer) || ArrayBuffer.isView(buffer);
}

toArrayBuffer(buffer: Bufferlike | WordArrayLike): ArrayBuffer {
toArrayBuffer(buffer: Bufferlike): ArrayBuffer {
const nodeBuffer = this.toBuffer(buffer);
return nodeBuffer.buffer.slice(nodeBuffer.byteOffset, nodeBuffer.byteOffset + nodeBuffer.byteLength);
}
Expand All @@ -73,16 +72,6 @@ class BufferUtils implements IBufferUtils<Bufferlike, Output, ToBufferOutput, Co
utf8Encode(string: string): Output {
return Buffer.from(string, 'utf8');
}

// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
toWordArray(buffer: TypedArray | WordArrayLike | number[] | ArrayBuffer): never {
throw new Error('Not implemented');
}

// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
isWordArray(val: unknown): val is never {
return false;
}
}

export default new BufferUtils();
33 changes: 2 additions & 31 deletions src/platform/web/lib/util/bufferutils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import WordArray from 'crypto-js/build/lib-typedarrays';
import Platform from 'common/platform';
import { TypedArray } from 'common/types/IPlatformConfig';
import IBufferUtils from 'common/types/IBufferUtils';
Expand All @@ -11,16 +10,11 @@ export type Bufferlike = ArrayBuffer | TypedArray;
export type Output = Bufferlike;
export type ToBufferOutput = Uint8Array;
export type ComparableBuffer = ArrayBuffer;
export type WordArrayLike = WordArray;

class BufferUtils implements IBufferUtils<Bufferlike, Output, ToBufferOutput, ComparableBuffer, WordArrayLike> {
class BufferUtils implements IBufferUtils<Bufferlike, Output, ToBufferOutput, ComparableBuffer> {
base64CharSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
hexCharSet = '0123456789abcdef';

isWordArray(ob: unknown): ob is WordArray {
return ob !== null && ob !== undefined && (ob as WordArray).sigBytes !== undefined;
}

isArrayBuffer(ob: unknown): ob is ArrayBuffer {
return ob !== null && ob !== undefined && (ob as ArrayBuffer).constructor === ArrayBuffer;
}
Expand Down Expand Up @@ -115,36 +109,13 @@ class BufferUtils implements IBufferUtils<Bufferlike, Output, ToBufferOutput, Co
throw new Error('BufferUtils.toBuffer expected an arraybuffer or typed array');
}

wordArrayToBuffer(wordArray: WordArrayLike) {
/* Backported from unreleased CryptoJS
* https://code.google.com/p/crypto-js/source/browse/branches/3.x/src/lib-typedarrays.js?r=661 */
var arrayBuffer = new ArrayBuffer(wordArray.sigBytes);
var uint8View = new Uint8Array(arrayBuffer);

for (var i = 0; i < wordArray.sigBytes; i++) {
uint8View[i] = (wordArray.words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
}

return uint8View;
}

toArrayBuffer(buffer: Bufferlike | WordArrayLike): ArrayBuffer {
toArrayBuffer(buffer: Bufferlike): ArrayBuffer {
if (this.isArrayBuffer(buffer)) {
return buffer;
}
if (this.isWordArray(buffer)) {
return this.wordArrayToBuffer(buffer);
}
return this.toBuffer(buffer).buffer;
}

toWordArray(buffer: Bufferlike | number[]) {
if (this.isTypedArray(buffer)) {
buffer = buffer.buffer;
}
return this.isWordArray(buffer) ? buffer : WordArray.create(buffer as number[]);
}

base64Encode(buffer: Bufferlike) {
return this.uint8ViewToBase64(this.toBuffer(buffer));
}
Expand Down

0 comments on commit 2357cc0

Please sign in to comment.