Socket
Socket
Sign inDemoInstall

ion-hash-js

Package Overview
Dependencies
3
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.0-SNAPSHOT to 1.0.2

.gitmodules

31

Gruntfile.js

@@ -0,1 +1,15 @@

/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
module.exports = function(grunt) {

@@ -10,13 +24,11 @@ grunt.loadNpmTasks('grunt-contrib-clean');

clean: ['dist', 'docs'],
clean: ['build', 'dist', 'docs'],
ts: {
tsconfig: 'tsconfig.json',
'commonjs-es5': {
outDir: 'dist/commonjs/es5',
options: {
target: 'es5',
module: 'commonjs',
},
tsconfig: 'tsconfig.commonjs.json',
},
'tests': {
tsconfig: 'tsconfig.tests.json',
},
},

@@ -29,4 +41,3 @@

suites: [
'dist/commonjs/es5/**/*.js',
'!dist/commonjs/es5/**/BigListOfNaughtyStringsTests.js', // https://github.com/amzn/ion-hash-js/issues/6
'build/tests/*.js',
],

@@ -55,3 +66,3 @@ },

grunt.registerTask('build', ['clean', 'ts:commonjs-es5']);
grunt.registerTask('test', ['build', 'intern:es5']);
grunt.registerTask('test', ['build', 'ts:tests', 'intern:es5']);
grunt.registerTask('doc', ['typedoc']);

@@ -58,0 +69,0 @@

{
"name": "ion-hash-js",
"version": "0.1.0-SNAPSHOT",
"version": "1.0.2",
"description": "JavaScript implementation of Amazon Ion Hash",

@@ -32,2 +32,3 @@ "main": "dist/commonjs/es5/IonHash.js",

"typedoc": "^0.14.2",
"typedoc-plugin-no-inherit": "^1.1.10",
"typescript": "^3.5.3"

@@ -37,5 +38,4 @@ },

"crypto": "^1.0.1",
"ion-js": "^3.0.0"
"ion-js": "^3.1.2"
}
}

@@ -40,4 +40,4 @@ ## ion-hash-js

process.stdout.write('digest: ');
digest.forEach(b => {
process.stdout.write(('0' + ((b as number) & 0xFF).toString(16)).slice(-2) + ' ');
digest.forEach((b: number) => {
process.stdout.write(('0' + (b & 0xFF).toString(16)).slice(-2) + ' ');
});

@@ -54,18 +54,18 @@ process.stdout.write('\n');

import * as ion from 'ion-js';
import {IonTypes} from 'ion-js';
import {cryptoIonHasherProvider, makeHashWriter} from 'ion-hash-js';
let hashWriter = makeHashWriter(
ion.makeTextWriter(),
cryptoIonHasherProvider('md5'));
hashWriter.writeList();
hashWriter.stepIn(IonTypes.LIST);
hashWriter.writeInt(1);
hashWriter.writeInt(2);
hashWriter.writeInt(3);
hashWriter.endContainer();
hashWriter.stepOut();
let digest = hashWriter.digest();
process.stdout.write('digest: ');
digest.forEach(b => {
process.stdout.write(('0' + ((b as number) & 0xFF)
.toString(16)).slice(-2) + ' ');
digest.forEach((b: number) => {
process.stdout.write(('0' + (b & 0xFF).toString(16)).slice(-2) + ' ');
});

@@ -79,1 +79,27 @@ process.stdout.write('\n');

## Development
This repository contains a [git submodule](https://git-scm.com/docs/git-submodule)
called `ion-hash-test`, which holds test data used by `ion-hash-js`'s unit tests.
The easiest way to clone the `ion-hash-js` repository and initialize its `ion-hash-test`
submodule is to run the following command:
```
$ git clone --recursive https://github.com/amzn/ion-hash-js.git ion-hash-js
```
Alternatively, the submodule may be initialized independently from the clone
by running the following commands:
```
$ git submodule init
$ git submodule update
```
## Known Issues
Any tests commented out in [tests/ion_hash_tests.ion](https://github.com/amzn/ion-hash-js/blob/master/tests/ion_hash_tests.ion)
are not expected to work at this time.

@@ -0,1 +1,16 @@

/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
// https://github.com/amzn/ion-hash-js/issues/7

@@ -2,0 +17,0 @@ //

@@ -1,16 +0,38 @@

import {Decimal, IonType, IonTypes, makeBinaryWriter,
Reader as IonReader, ReaderScalarValue, Timestamp, Writer as IonWriter} from 'ion-js';
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import {Decimal, IntSize, IonType, IonTypes, makeBinaryWriter,
Reader, ReaderScalarValue, Timestamp, Writer} from 'ion-js';
import {createHash, Hash} from 'crypto';
import {IonHasher, IonHasherProvider, IonHashReader, IonHashWriter} from "../IonHash";
import {Hasher, HasherProvider, HashReader, HashWriter} from "../IonHash";
import JSBI from "jsbi";
export class _CryptoIonHasher implements IonHasher {
private readonly _hash: Hash;
export class _CryptoHasher implements Hasher {
private _hash: Hash;
constructor(algorithm: string) {
constructor(private readonly algorithm: string) {
this._hash = createHash(algorithm);
}
update(bytes: Uint8Array): void { this._hash.update(bytes) }
digest(): Uint8Array { return this._hash.digest() }
update(bytes: Uint8Array): void {
this._hash.update(bytes);
}
digest(): Uint8Array {
let digest = this._hash.digest();
this._hash = createHash(this.algorithm);
return digest;
}
}

@@ -27,13 +49,14 @@

export class _HashReaderImpl implements IonHashReader, _IonValue {
export class _HashReaderImpl implements HashReader, _IonValue {
private readonly _hasher: _Hasher;
private _ionType: IonType | null = null;
constructor(private readonly _reader: IonReader,
private readonly _hashFunctionProvider: IonHasherProvider) {
constructor(private readonly _reader: Reader,
private readonly _hashFunctionProvider: HasherProvider) {
this._hasher = new _Hasher(this._hashFunctionProvider);
}
// implements IonReader
// implements Reader
annotations() : string[] { return this._reader.annotations() }
bigIntValue() : JSBI | null { return this._reader.bigIntValue() }
booleanValue() : boolean | null { return this._reader.booleanValue() }

@@ -44,2 +67,3 @@ byteValue() : Uint8Array | null { return this._reader.byteValue() }

fieldName() : string | null { return this._reader.fieldName() }
intSize() : IntSize { return this._reader.intSize() }
isNull() : boolean { return this._reader.isNull() }

@@ -93,3 +117,3 @@ numberValue() : number | null { return this._reader.numberValue() }

// implements IonHashReader
// implements HashReader
digest(): Uint8Array { return this._hasher._digest() }

@@ -105,3 +129,3 @@

export class _HashWriterImpl implements IonHashWriter, _IonValue {
export class _HashWriterImpl implements HashWriter, _IonValue {
private readonly _hasher: _Hasher;

@@ -115,4 +139,4 @@

constructor(private readonly _writer: IonWriter,
private readonly _hashFunctionProvider: IonHasherProvider) {
constructor(private readonly _writer: Writer,
private readonly _hashFunctionProvider: HasherProvider) {
this._hasher = new _Hasher(this._hashFunctionProvider);

@@ -166,3 +190,3 @@ }

}
writeInt(value: number | null): void {
writeInt(value: number | JSBI | null): void {
this._hashScalar(IonTypes.INT, value);

@@ -211,7 +235,7 @@ this._writer.writeInt(value);

writeValue(reader: IonReader): void {
writeValue(reader: Reader): void {
this._writeValue(reader);
}
private _writeValue(reader: IonReader, _depth = 0): void {
private _writeValue(reader: Reader, _depth = 0): void {
let type: IonType | null = reader.type();

@@ -233,3 +257,3 @@ if (type === null) {

case IonTypes.BOOL: this.writeBoolean(reader.booleanValue()); break;
case IonTypes.INT: this.writeInt(reader.numberValue()); break;
case IonTypes.INT: this.writeInt(reader.bigIntValue()); break;
case IonTypes.FLOAT: this.writeFloat64(reader.numberValue()); break;

@@ -256,7 +280,7 @@ case IonTypes.DECIMAL: this.writeDecimal(reader.decimalValue()); break;

writeValues(reader: IonReader): void {
writeValues(reader: Reader): void {
this._writeValues(reader);
}
private _writeValues(reader: IonReader, _depth = 0): void {
private _writeValues(reader: Reader, _depth = 0): void {
let type: IonType | null = reader.type();

@@ -274,4 +298,5 @@ if (type === null) {

close (): void { }
depth (): number { return this._writer.depth() }
// implements IonHashWriter
// implements HashWriter
digest(): Uint8Array { return this._hasher._digest() }

@@ -291,3 +316,3 @@

constructor(private readonly _ihp: IonHasherProvider) {
constructor(private readonly _ihp: HasherProvider) {
this._currentHasher = new _Serializer(this._ihp(), 0);

@@ -345,13 +370,13 @@ this._hasherStack.push(this._currentHasher);

class _Serializer {
private static readonly _serializers: { [typeName: string]: (value: any, writer: IonWriter) => void } = {
"null": (value: any, writer: IonWriter) => { writer.writeNull(IonTypes.NULL) },
"bool": (value: any, writer: IonWriter) => { writer.writeBoolean(value) },
"int": (value: any, writer: IonWriter) => { writer.writeInt(value) },
"float": (value: any, writer: IonWriter) => { writer.writeFloat64(value) },
"decimal": (value: any, writer: IonWriter) => { writer.writeDecimal(value) },
"timestamp": (value: any, writer: IonWriter) => { writer.writeTimestamp(value) },
"symbol": (value: any, writer: IonWriter) => { writer.writeString(value) },
"string": (value: any, writer: IonWriter) => { writer.writeString(value) },
"clob": (value: any, writer: IonWriter) => { writer.writeClob(value) },
"blob": (value: any, writer: IonWriter) => { writer.writeBlob(value) },
private static readonly _serializers: { [typeName: string]: (value: any, writer: Writer) => void } = {
"null": (value: any, writer: Writer) => { writer.writeNull(IonTypes.NULL) },
"bool": (value: any, writer: Writer) => { writer.writeBoolean(value) },
"int": (value: any, writer: Writer) => { writer.writeInt(value) },
"float": (value: any, writer: Writer) => { writer.writeFloat64(value) },
"decimal": (value: any, writer: Writer) => { writer.writeDecimal(value) },
"timestamp": (value: any, writer: Writer) => { writer.writeTimestamp(value) },
"symbol": (value: any, writer: Writer) => { writer.writeString(value) },
"string": (value: any, writer: Writer) => { writer.writeString(value) },
"clob": (value: any, writer: Writer) => { writer.writeClob(value) },
"blob": (value: any, writer: Writer) => { writer.writeBlob(value) },
};

@@ -361,3 +386,3 @@

constructor(public _hashFunction: IonHasher, private readonly _depth: number) {
constructor(public _hashFunction: Hasher, private readonly _depth: number) {
}

@@ -378,2 +403,6 @@

for (let annotation of annotations) {
// https://github.com/amzn/ion-hash-js/issues/29
if (annotation === '$0') {
throw new Error("Symbol ID '$0' is not currently supported.");
}
this._writeSymbol(annotation);

@@ -495,8 +524,8 @@ }

class _StructSerializer extends _Serializer {
_scalarSerializer: _Serializer;
_fieldHashes: Uint8Array[] = [];
private readonly _scalarSerializer: _Serializer;
private readonly _fieldHashes: Uint8Array[] = [];
constructor(hashFunction: IonHasher,
constructor(hashFunction: Hasher,
depth: number,
hashFunctionProvider: IonHasherProvider) {
hashFunctionProvider: HasherProvider) {
super(hashFunction, depth);

@@ -503,0 +532,0 @@ this._scalarSerializer = new _Serializer(hashFunctionProvider(), depth + 1);

@@ -1,8 +0,22 @@

import {Reader as IonReader} from 'ion-js';
import {Writer as IonWriter} from 'ion-js';
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import {_HashReaderImpl, _HashWriterImpl, _CryptoIonHasher} from './internal/IonHashImpl';
import {Reader, Writer} from 'ion-js';
import {_HashReaderImpl, _HashWriterImpl, _CryptoHasher} from './internal/IonHashImpl';
/**
* Wraps the provided IonReader as an IonHashReader.
* Wraps the provided Reader as a HashReader.
*

@@ -12,4 +26,4 @@ * @param reader

*/
export function makeHashReader(reader: IonReader,
hashFunctionProvider: IonHasherProvider): IonHashReader {
export function makeHashReader(reader: Reader,
hashFunctionProvider: HasherProvider): HashReader {
return new _HashReaderImpl(reader, hashFunctionProvider);

@@ -19,3 +33,3 @@ }

/**
* Wraps the provided IonWriter as an IonHashWriter.
* Wraps the provided Writer as a HashWriter.
*

@@ -25,4 +39,4 @@ * @param writer

*/
export function makeHashWriter(writer: IonWriter,
hashFunctionProvider: IonHasherProvider): IonHashWriter {
export function makeHashWriter(writer: Writer,
hashFunctionProvider: HasherProvider): HashWriter {
return new _HashWriterImpl(writer, hashFunctionProvider);

@@ -33,9 +47,10 @@ }

/**
* IonReader decorator that computes the Ion hash of values read.
* Reader decorator that computes the Ion hash of values read.
*
* @see Reader
* @see [Reader](https://amzn.github.io/ion-js/api/interfaces/_ionreader_.reader.html)
* @noInheritDoc
*/
export interface IonHashReader extends IonReader {
export interface HashReader extends Reader {
/**
* Provides the Ion hash of the previous value (where IonReader.next()
* Provides the Ion hash of the previous value (where Reader.next()
* positions the reader at a new current value).

@@ -48,3 +63,3 @@ *

* @return bytes representing the Ion hash of the previous value
* @throws if invoked at a different depth than when the IonHashReader was instantiated
* @throws if invoked at a different depth than when the HashReader was instantiated
*/

@@ -55,7 +70,8 @@ digest(): Uint8Array;

/**
* IonWriter decorator that computes the Ion hash of written values.
* Writer decorator that computes the Ion hash of written values.
*
* @see Writer
* @see [Writer](https://amzn.github.io/ion-js/api/interfaces/_ionwriter_.writer.html)
* @noInheritDoc
*/
export interface IonHashWriter extends IonWriter {
export interface HashWriter extends Writer {
/**

@@ -65,3 +81,3 @@ * Provides the Ion hash of the value just written.

* @return bytes representing the Ion hash of the value just written
* @throws if invoked at a different depth than when the IonHashWriter was instantiated
* @throws if invoked at a different depth than when the HashWriter was instantiated
*/

@@ -73,9 +89,9 @@ digest(): Uint8Array;

/**
* Implementations of this function type interface create an IonHasher
* Implementations of this function type interface create a Hasher
* instance when invoked.
*
* @see [[cryptoIonHasherProvider]]
* @see [[cryptoHasherProvider]]
*/
export interface IonHasherProvider {
(): IonHasher;
export interface HasherProvider {
(): Hasher;
}

@@ -87,3 +103,3 @@

*/
export interface IonHasher {
export interface Hasher {
/**

@@ -106,3 +122,3 @@ * Updates the hash with the specified array of bytes.

/**
* An IonHasherProvider implementation backed by node's [crypto](https://node.readthedocs.io/en/latest/api/crypto/)
* A HasherProvider implementation backed by node's [crypto](https://node.readthedocs.io/en/latest/api/crypto/)
* module.

@@ -114,5 +130,5 @@ *

*/
export function cryptoIonHasherProvider(algorithm: string): IonHasherProvider {
return (): IonHasher => { return new _CryptoIonHasher(algorithm) };
export function cryptoHasherProvider(algorithm: string): HasherProvider {
return (): Hasher => { return new _CryptoHasher(algorithm) };
}

@@ -0,1 +1,16 @@

/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import {makeBinaryWriter, makeReader} from 'ion-js';

@@ -7,4 +22,4 @@

import {IonHashReader, IonHashWriter, makeHashReader, makeHashWriter} from '../src/IonHash';
import {testIonHasherProvider, toHexString, writeln} from './testutil';
import {HashReader, HashWriter, makeHashReader, makeHashWriter} from '../src/IonHash';
import {testHasherProvider, toHexString, writeln} from './testutil';

@@ -17,3 +32,3 @@ class TestValue {

constructor(public inputValue: string) {
constructor(public readonly inputValue: string) {
this.validIon = null;

@@ -34,4 +49,4 @@

let s = this.inputValue;
s = s.replace("\\", "\\\\");
s = s.replace("'", "\\'");
s = s.replace(/\\/g, '\\\\');
s = s.replace(/'/g, "\\'");
return "\'" + s + "\'";

@@ -42,4 +57,4 @@ }

let s = this.inputValue;
s = s.replace("\\", "\\\\");
s = s.replace("\"", "\\\"");
s = s.replace(/\\/g, '\\\\');
s = s.replace(/"/g, '\\\"');
return "\"" + s + "\"";

@@ -50,4 +65,4 @@ }

let s = this.inputValue;
s = s.replace("\\", "\\\\");
s = s.replace("'", "\\'");
s = s.replace(/\\/g, '\\\\');
s = s.replace(/'/g, "\\'");
return "'''" + s + "'''";

@@ -76,5 +91,5 @@ }

// build the suite based on the contents of big-list-of-naughty-strings.txt
let strings: string[] = [];
readFileSync('tests/big-list-of-naughty-strings.txt', 'utf-8')
// build the suite based on the contents of big_list_of_naughty_strings.txt
let suite: { [testName: string]: () => void } = { };
readFileSync('ion-hash-test/big_list_of_naughty_strings.txt', 'utf-8')
.split(/\r?\n/)

@@ -85,2 +100,3 @@ .filter(line => !(line == '' || line[0] == '#'))

let strings: string[] = [];
strings.push(tv.symbol());

@@ -135,14 +151,8 @@ strings.push(tv.string());

strings.push(tv.symbol() + "::" + tv.symbol() + "::" + tv.symbol() + "::" + tv.string());
strings.forEach(str => {
suite[str] = () => runTest(tv, str);
});
});
let suite: { [testName: string]: () => void } = { };
let testCount = 0;
strings.forEach(str => {
suite[str] = () => {
runTest(str);
};
testCount++;
});
writeln("testCount: " + testCount);
// ask intern to execute the tests

@@ -152,11 +162,11 @@ registerSuite('BigListOfNaughtyStringsTests', suite);

function runTest(testString: string) {
let tv = new TestValue(testString);
let hashWriter: IonHashWriter | null = null;
function runTest(tv: TestValue, testString: string) {
let hashWriter: HashWriter;
try {
let reader = makeReader(tv.inputValue);
let reader = makeReader(testString);
hashWriter = makeHashWriter(makeBinaryWriter(), testHasherProvider('md5'));
reader.next();
hashWriter = makeHashWriter(makeBinaryWriter(), testIonHasherProvider('identity'));
hashWriter.writeValue(reader);
} catch (e) {
writeln(e);
if (tv.validIon) {

@@ -167,6 +177,6 @@ throw e;

let hashReader: IonHashReader | null = null;
let hashReader: HashReader;
try {
let reader = makeReader(tv.inputValue);
hashReader = makeHashReader(reader, testIonHasherProvider('identity'));
let reader = makeReader(testString);
hashReader = makeHashReader(reader, testHasherProvider('md5'));
hashReader.next();

@@ -173,0 +183,0 @@ hashReader.next();

@@ -0,1 +1,16 @@

/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
const {registerSuite} = intern.getPlugin('interface.object');

@@ -2,0 +17,0 @@ const {assert} = intern.getPlugin('chai');

@@ -0,1 +1,16 @@

/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import {IonTypes, makeReader, makeTextWriter} from "ion-js";

@@ -7,3 +22,3 @@

import {makeHashReader, makeHashWriter} from '../src/IonHash';
import {sexpStringToBytes, testIonHasherProvider} from './testutil';
import {sexpStringToBytes, testHasherProvider} from './testutil';

@@ -36,3 +51,3 @@ registerSuite('FieldnameBehavior', {

// verify IonHashWriter behavior:
// verify HashWriter behavior:
//let writer = makeBinaryWriter(); // https://github.com/amzn/ion-hash-js/issues/2

@@ -42,3 +57,3 @@ let writer = makeTextWriter();

let hashWriter = makeHashWriter(writer, testIonHasherProvider('identity'));
let hashWriter = makeHashWriter(writer, testHasherProvider('identity'));
hashWriter.writeFieldName("field_name"); // this fieldName should not become part of the hash

@@ -60,3 +75,3 @@

// verify IonHashReader behavior:
// verify HashReader behavior:
reader = makeReader(bytes);

@@ -66,3 +81,3 @@ reader.next();

let hashReader = makeHashReader(reader, testIonHasherProvider('identity'));
let hashReader = makeHashReader(reader, testHasherProvider('identity'));
hashReader.next();

@@ -69,0 +84,0 @@ hashReader.next();

@@ -0,1 +1,16 @@

/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import {Tests} from "intern/lib/interfaces/object";

@@ -7,5 +22,5 @@

import {makeBinaryWriter, makeReader, makeTextWriter, Reader as IonReader, Writer as IonWriter} from 'ion-js';
import {makeBinaryWriter, makeReader, makeTextWriter, Reader, Writer} from 'ion-js';
import {makeHashReader, makeHashWriter} from '../src/IonHash';
import {sexpToBytes, testIonHasherProvider, toHexString, readerToString, writeln} from './testutil';
import {sexpToBytes, testHasherProvider, toHexString, readerToString, writeln} from './testutil';

@@ -33,4 +48,4 @@ // builds a test suite based on the contents of ion_hash_tests.ion

function readerDigester(reader: IonReader, algorithm: string, hasherLog: string[]): void {
function traverse(reader: IonReader) {
function readerDigester(reader: Reader, algorithm: string, hasherLog: string[]): void {
function traverse(reader: Reader) {
for (let type; type = reader.next(); ) {

@@ -44,3 +59,3 @@ if (type.isContainer && !reader.isNull()) {

}
let hashReader = makeHashReader(reader, testIonHasherProvider(algorithm, hasherLog));
let hashReader = makeHashReader(reader, testHasherProvider(algorithm, hasherLog));
traverse(hashReader);

@@ -53,3 +68,3 @@ hashReader.digest();

makeReader(ionData),
testIonHasherProvider(algorithm, hasherLog));
testHasherProvider(algorithm, hasherLog));
hashReader.next();

@@ -68,6 +83,6 @@ hashReader.next();

function writerDigester(writer: IonWriter, ionData: string | Uint8Array, algorithm: string, hasherLog: string[]): void {
function writerDigester(writer: Writer, ionData: string | Uint8Array, algorithm: string, hasherLog: string[]): void {
let reader = makeReader(ionData);
reader.next();
let hashWriter = makeHashWriter(writer, testIonHasherProvider(algorithm, hasherLog));
let hashWriter = makeHashWriter(writer, testHasherProvider(algorithm, hasherLog));
hashWriter.writeValue(reader);

@@ -90,20 +105,19 @@ hashWriter.digest();

let expectedIonHasherLog = getExpectedIonHasherLog(expect);
let actualIonHasherLog: string[] = [];
let expectedHasherLog = getExpectedHasherLog(expect);
let actualHasherLog: string[] = [];
digester(ionData, algorithm, actualIonHasherLog);
digester(ionData, algorithm, actualHasherLog);
if (expectedIonHasherLog.length == 1
&& expectedIonHasherLog[0].startsWith('final_digest::')) {
assert.deepEqual('final_' + actualIonHasherLog.pop(), expectedIonHasherLog[0]);
if (expectedHasherLog.length == 1
&& expectedHasherLog[0].startsWith('final_digest::')) {
assert.deepEqual('final_' + actualHasherLog.pop(), expectedHasherLog[0]);
} else {
if (algorithm == 'md5') {
expectedIonHasherLog = expectedIonHasherLog.filter(
expectedHasherLog = expectedHasherLog.filter(
entry => entry.startsWith('digest::'));
}
assert.deepEqual(actualIonHasherLog, expectedIonHasherLog);
assert.deepEqual(actualHasherLog, expectedHasherLog);
}
}
//let suites: { [digesterName: string]: [testName: string]: () => void } = { };
let suites: { [digesterName: string]: Tests } = { };

@@ -173,3 +187,3 @@ for (const digester in digesters) {

function getExpectedIonHasherLog(expect: string): string[] {
function getExpectedHasherLog(expect: string): string[] {
let log: string[] = [];

@@ -176,0 +190,0 @@ let reader = makeReader(expect);

@@ -1,7 +0,22 @@

import {IonType, IonTypes, makeReader, makeTextWriter, Reader as IonReader, Writer as IonWriter} from 'ion-js';
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import {IonHasher, IonHasherProvider} from "../src/IonHash";
import {makeReader, makeTextWriter, Reader} from 'ion-js';
import {Hasher, HasherProvider} from "../src/IonHash";
import {createHash, Hash} from 'crypto';
class IdentityIonHasher implements IonHasher {
class IdentityHasher implements Hasher {
private allBytes: number[] = [];

@@ -27,3 +42,3 @@

class CryptoTestIonHasher implements IonHasher {
class CryptoTestHasher implements Hasher {
private hash: Hash;

@@ -47,8 +62,8 @@

export function testIonHasherProvider(algorithm: string, log?: string[]): IonHasherProvider {
return (): IonHasher => {
export function testHasherProvider(algorithm: string, log?: string[]): HasherProvider {
return (): Hasher => {
if (algorithm == 'identity') {
return new IdentityIonHasher(log);
return new IdentityHasher(log);
} else {
return new CryptoTestIonHasher(algorithm, log);
return new CryptoTestHasher(algorithm, log);
}

@@ -65,3 +80,3 @@ };

export function sexpToBytes(reader: IonReader, options = { asIonBinary: false }): Uint8Array {
export function sexpToBytes(reader: Reader, options = { asIonBinary: false }): Uint8Array {
let bytes: number[];

@@ -82,5 +97,6 @@ if (options.asIonBinary) {

export function readerToString(reader: IonReader): string {
export function readerToString(reader: Reader): string {
let writer = makeTextWriter();
writer.writeValue(reader);
writer.close();
return String.fromCharCode.apply(null, writer.getBytes());

@@ -87,0 +103,0 @@ }

@@ -0,1 +1,16 @@

/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
const {registerSuite} = intern.getPlugin('interface.object');

@@ -2,0 +17,0 @@ const {assert} = intern.getPlugin('chai');

{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"noImplicitAny": true,
"rootDirs": ["src", "tests"],
"sourceMap": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"types": ["intern"],
"verbose": true
},
"include": [
"src/**/*.ts",
"tests/**/*.ts"
]
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc