Skip to content

Commit

Permalink
Updates crypto library
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Niemann committed Oct 27, 2019
1 parent b123169 commit d9c0a66
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 78 deletions.
77 changes: 28 additions & 49 deletions LICENSE.md
Expand Up @@ -82,6 +82,34 @@ Repository: https://github.com/acornjs/acorn.git
---------------------------------------

## asmcrypto.js
License: MIT
By: Ádám Lippai, Artem S Vybornov, Ximin Luo
Repository: git+https://github.com/asmcrypto/asmcrypto.js.git

> The MIT License (MIT)
>
> Copyright (c) 2013 Artem S Vybornov
>
> Permission is hereby granted, free of charge, to any person obtaining a copy of
> this software and associated documentation files (the "Software"), to deal in
> the Software without restriction, including without limitation the rights to
> use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
> the Software, and to permit persons to whom the Software is furnished to do so,
> subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in all
> copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
> FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
> COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
> IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
> CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
---------------------------------------

## braces
License: MIT
By: Jon Schlinkert, Brian Woodward, Elan Shanker, Eugene Sharygin, hemanth.hm
Expand Down Expand Up @@ -147,35 +175,6 @@ Repository: jonschlinkert/fill-range
---------------------------------------

## hash.js
License: MIT
By: Fedor Indutny
Repository: git@github.com:indutny/hash.js

---------------------------------------

## inherits
License: ISC
Repository: git://github.com/isaacs/inherits

> The ISC License
>
> Copyright (c) Isaac Z. Schlueter
>
> Permission to use, copy, modify, and/or distribute this software for any
> purpose with or without fee is hereby granted, provided that the above
> copyright notice and this permission notice appear in all copies.
>
> THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
> REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
> FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
> INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
> LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
> OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
> PERFORMANCE OF THIS SOFTWARE.
---------------------------------------

## is-number
License: MIT
By: Jon Schlinkert, Olsten Larck, Rouven Weßling
Expand Down Expand Up @@ -263,26 +262,6 @@ Repository: micromatch/micromatch
---------------------------------------

## minimalistic-assert
License: ISC
Repository: https://github.com/calvinmetcalf/minimalistic-assert.git

> Copyright 2015 Calvin Metcalf
>
> Permission to use, copy, modify, and/or distribute this software for any purpose
> with or without fee is hereby granted, provided that the above copyright notice
> and this permission notice appear in all copies.
>
> THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
> REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
> FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
> INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
> LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
> OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
> PERFORMANCE OF THIS SOFTWARE.
---------------------------------------

## minimist
License: MIT
By: James Halliday
Expand Down
49 changes: 49 additions & 0 deletions browser/crypto.ts
@@ -0,0 +1,49 @@
import * as asmCrypto from 'asmcrypto.js';

function createHash(algorithm: string) {
return new Hasher(algorithm);
}

class Hasher {
hasher: asmCrypto.Sha256;

constructor(algorithm: string) {
switch (algorithm) {
case 'sha256':
this.hasher = new asmCrypto.Sha256();
break;
default:
throw new Error(`Unsupported algorithm: '${algorithm}'.`);
}
}

digest(encoding: string) {
this.hasher.finish();
// The type of '.result' is always 'Uint8Array' after calling 'finish()'. Before that, it could also be 'null'. Casting
// is necessary to avoid type errors when encoding the hash.
const hash = this.hasher.result as Uint8Array;

let encoded: string;
switch (encoding) {
case 'hex':
encoded = asmCrypto.bytes_to_hex(hash);
break;
default:
throw new Error(`Unsupported encoding: '${encoding}'.`);
}

return encoded;
}

update(data: string | Buffer) {
if (typeof data === 'string') {
this.hasher.process(new TextEncoder().encode(data));
} else {
this.hasher.process(data);
}

return this;
}
}

export { createHash };
22 changes: 6 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -75,6 +75,7 @@
"acorn-import-meta": "^1.0.0",
"acorn-jsx": "^5.1.0",
"acorn-walk": "^7.0.0",
"asmcrypto.js": "^2.3.2",
"buble": "^0.19.8",
"chokidar": "^2.1.8",
"codecov": "^3.6.1",
Expand All @@ -88,7 +89,6 @@
"eslint-plugin-import": "^2.18.2",
"execa": "^3.2.0",
"fixturify": "^1.2.0",
"hash.js": "^1.1.7",
"husky": "^3.0.9",
"is-reference": "^1.1.4",
"lint-staged": "^9.4.2",
Expand Down
3 changes: 2 additions & 1 deletion rollup.config.js
Expand Up @@ -171,7 +171,7 @@ export default command => {
!command.configTest && license({ thirdParty: generateLicenseFile })
],
// acorn needs to be external as some plugins rely on a shared acorn instance
external: ['acorn', 'assert', 'events', 'fs', 'module', 'path', 'util'],
external: ['acorn', 'assert', 'crypto', 'events', 'fs', 'module', 'path', 'util'],
treeshake,
output: {
banner,
Expand Down Expand Up @@ -205,6 +205,7 @@ export default command => {
json(),
{
load: id => {
if (~id.indexOf('crypto.ts')) return fs.readFileSync('browser/crypto.ts', 'utf-8');
if (~id.indexOf('fs.ts')) return fs.readFileSync('browser/fs.ts', 'utf-8');
if (~id.indexOf('path.ts')) return fs.readFileSync('browser/path.ts', 'utf-8');
}
Expand Down
6 changes: 3 additions & 3 deletions src/Chunk.ts
@@ -1,5 +1,5 @@
import sha256 from 'hash.js/lib/hash/sha/256';
import MagicString, { Bundle as MagicStringBundle, SourceMap } from 'magic-string';
import { createHash } from '../browser/crypto';
import { relative } from '../browser/path';
import { createInclusionContext } from './ast/ExecutionContext';
import ExportDefaultDeclaration from './ast/nodes/ExportDefaultDeclaration';
Expand Down Expand Up @@ -366,7 +366,7 @@ export default class Chunk {
getRenderedHash(): string {
if (this.renderedHash) return this.renderedHash;
if (!this.renderedSource) return '';
const hash = sha256();
const hash = createHash('sha256');
const hashAugmentation = this.calculateHashAugmentation();
hash.update(hashAugmentation);
hash.update(this.renderedSource.toString());
Expand Down Expand Up @@ -860,7 +860,7 @@ export default class Chunk {
options: OutputOptions,
existingNames: Record<string, any>
): string {
const hash = sha256();
const hash = createHash('sha256');
hash.update(
[addons.intro, addons.outro, addons.banner, addons.footer].map(addon => addon || '').join(':')
);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/FileEmitter.ts
@@ -1,9 +1,9 @@
import sha256 from 'hash.js/lib/hash/sha/256';
import Chunk from '../Chunk';
import Graph from '../Graph';
import Module from '../Module';
import { FilePlaceholder, OutputBundleWithPlaceholders } from '../rollup/types';
import { BuildPhase } from './buildPhase';
import { createHash } from './crypto';
import {
errAssetNotFinalisedForFileName,
errAssetReferenceIdNotFoundForSetSource,
Expand Down Expand Up @@ -33,7 +33,7 @@ function generateAssetFileName(
return makeUnique(
renderNamePattern(output.assetFileNames, 'output.assetFileNames', {
hash() {
const hash = sha256();
const hash = createHash('sha256');
hash.update(emittedName);
hash.update(':');
hash.update(source);
Expand Down
1 change: 1 addition & 0 deletions src/utils/crypto.ts
@@ -0,0 +1 @@
export { createHash } from 'crypto';
6 changes: 0 additions & 6 deletions typings/Chunk.d.ts

This file was deleted.

0 comments on commit d9c0a66

Please sign in to comment.