Skip to content

Commit

Permalink
ib/crc32-accelerate
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed Aug 5, 2020
1 parent eeea959 commit 4c67d0a
Show file tree
Hide file tree
Showing 18 changed files with 633 additions and 26 deletions.
6 changes: 5 additions & 1 deletion modules/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
"@babel/runtime": "^7.3.1",
"@loaders.gl/loader-utils": "2.3.0-alpha.7",
"@types/crypto-js": "*",
"crypto-js": "^3.0.0 || ^4.0.0"
"crypto-js": "^3.0.0 || ^4.0.0",
"sse4_crc32": "^6.0.1"
},
"browser": {
"sse4_crc32": false
}
}
4 changes: 4 additions & 0 deletions modules/crypto/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// Cryptographic hash
export {default as CryptoHashTransform} from './lib/crypto/crypto-hash-transform';
export {default as CRC32HashTransform} from './lib/crc32/crc32-hash-transform';
export {default as CRC32CHashTransform} from './lib/crc32c/crc32c-hash-transform';
export {default as MD5HashTransform} from './lib/md5-wasm/md5-hash-transform';

export {NullLoader} from './lib/utils/null-loader';
export {hexToBase64 as _hexToBase64, toHex as _toHex} from './lib/utils/base64-utils';
4 changes: 4 additions & 0 deletions modules/crypto/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export {default as CryptoHashTransform} from './lib/crypto/crypto-hash-transform';
export {default as CRC32HashTransform} from './lib/crc32/crc32-hash-transform';
export {default as CRC32CHashTransform} from './lib/crc32c/crc32c-hash-transform';
export {default as MD5HashTransform} from './lib/md5-wasm/md5-hash-transform';

export {NullLoader} from './lib/utils/null-loader';
export {hexToBase64 as _hexToBase64, toHex as _toHex} from './lib/utils/base64-utils';
7 changes: 4 additions & 3 deletions modules/crypto/src/lib/crc32/crc32.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// CRC32 doesn't appear to be supported natively by crypto-js
// https://gist.github.com/wqli78/1330293/6d85cc967f32cccfcbad94ae7d088a3dcfc14bd9
import {hexToBase64} from './hex-to-base64';
import {hexToBase64} from '../utils/base64-utils';

/**
* Calculates the CRC32 checksum of a string.
Expand Down Expand Up @@ -31,7 +31,8 @@ export default class CRC32 {
}
}

const CRC32TAB = [
// Note: Using a typed array here doubles the speed of the cipher
const CRC32TAB = Uint32Array.of(
0x00000000,
0x77073096,
0xee0e612c,
Expand Down Expand Up @@ -288,7 +289,7 @@ const CRC32TAB = [
0xc30c8ea1,
0x5a05df1b,
0x2d02ef8d
];
);

function getCRC32Table() {
return CRC32TAB;
Expand Down
19 changes: 19 additions & 0 deletions modules/crypto/src/lib/crc32c/crc32c-hash-transform.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {IncrementalTransform} from "@loaders.gl/loader-utils";

/**
* A transform that calcultes Cryptographic Hash
*/
export default class CRC32CHashTransform implements IncrementalTransform {
/**
* Atomic hash calculation
* @returns base64 encoded hash
*/
static hash(input: ArrayBuffer, options?: object): Promise<string>;
static hashSync(input: ArrayBuffer, options?: object): string;

constructor(options: object);

write(chunk: ArrayBuffer): ArrayBuffer | null;

end(): ArrayBuffer | null;
}
46 changes: 46 additions & 0 deletions modules/crypto/src/lib/crc32c/crc32c-hash-transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import CRC32C from './crc32c';
import {toHex, hexToBase64} from '../utils/base64-utils';

export default class CRC32CHashTransform {
static async hash(input, options) {
return CRC32CHashTransform.hashSync(input, options);
}

static hashSync(input, options) {
const transform = new CRC32CHashTransform(options);
return transform._update(input)._finish();
}

constructor(options) {
this.options = options;
this._hash = new CRC32C(options);
}

/**
* Pass-through. Update the hash and pass the chunk through unharmed
*/
write(chunk) {
this._update(chunk);
return chunk;
}

end() {
const hash = this._finish();
if (this.options.onEnd) {
this.options.onEnd({hash});
}
return hash;
}

_update(chunk) {
this._hash.update(chunk);
return this;
}

_finish() {
const hashValue = this._hash.finalize();
const hex = toHex(hashValue);
const hash = hexToBase64(hex);
return hash;
}
}

0 comments on commit 4c67d0a

Please sign in to comment.