Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v0.6.1: added WrappedKey de/serialization
- Loading branch information
1 parent
6400c39
commit 343a07c
Showing
7 changed files
with
69 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,3 +133,4 @@ fabric.properties | |
dist/**/* | ||
.rpt2_cache/**/* | ||
docs/**/* | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {Deserialize, Serialize} from '@esentri/de-serializer' | ||
import {InitializationVector, WrappedKey} from '../crypto-wrapper' | ||
import {Base64ToUint8Array, Uint8ArrayToBase64} from '@esentri/transformer-functions' | ||
|
||
export const WrappedKeySerialize: Serialize = (element: WrappedKey): Promise<WrappedKeySerialized> => { | ||
return Promise.resolve(new WrappedKeySerialized(element.base64, element.config, | ||
element.vector ? Uint8ArrayToBase64(element.vector.asArray()) : null)) | ||
} | ||
|
||
export class WrappedKeySerialized { | ||
|
||
public readonly base64: string | ||
public readonly config: any | ||
public readonly vectorBase64: string | null | ||
|
||
constructor (base64: string, config: any, vectorBase64: string | null) { | ||
this.base64 = base64 | ||
this.config = config | ||
this.vectorBase64 = vectorBase64 | ||
} | ||
} | ||
|
||
export const WrappedKeyDeserialize: Deserialize<WrappedKey> = (element: WrappedKeySerialized): Promise<WrappedKey> => { | ||
return Promise.resolve( | ||
new WrappedKey(element.base64, | ||
element.config, | ||
element.vectorBase64 ? new InitializationVector(Base64ToUint8Array(element.vectorBase64)) : null)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {symKeyBase64} from '../testData/Keys' | ||
import {InitializationVector, SymmetricKeyConfig, WrappedKey, WrappedKeyDeserialize, WrappedKeySerialize} from '../../src/crypto-wrapper' | ||
import {Uint8ArrayToBase64} from '@esentri/transformer-functions' | ||
|
||
describe('de-serialize WrappedKey', () => { | ||
|
||
it('de-serialize WrappedKey (without InitializationVector)', done => { | ||
let wrappedKey = new WrappedKey(symKeyBase64, SymmetricKeyConfig.DEFAULT) | ||
WrappedKeySerialize(wrappedKey).then(serializedWrappedKey => { | ||
expect(serializedWrappedKey.base64).toEqual(wrappedKey.base64) | ||
expect(serializedWrappedKey.config).toEqual(wrappedKey.config) | ||
expect(serializedWrappedKey.vectorBase64).toBeNull() | ||
WrappedKeyDeserialize(serializedWrappedKey).then(deserializedKey => { | ||
expect(deserializedKey.base64).toEqual(wrappedKey.base64) | ||
expect(deserializedKey.config).toEqual(wrappedKey.config) | ||
expect(deserializedKey.vector).toBeNull() | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
it('de-serialize WrappedKey (with InitializationVector)', done => { | ||
let wrappedKey = new WrappedKey(symKeyBase64, SymmetricKeyConfig.DEFAULT, InitializationVector.random()) | ||
WrappedKeySerialize(wrappedKey).then(serializedWrappedKey => { | ||
expect(serializedWrappedKey.base64).toEqual(wrappedKey.base64) | ||
expect(serializedWrappedKey.config).toEqual(wrappedKey.config) | ||
expect(serializedWrappedKey.vectorBase64).toEqual(Uint8ArrayToBase64(wrappedKey.vector!.asArray())) | ||
WrappedKeyDeserialize(serializedWrappedKey).then(deserializedKey => { | ||
expect(deserializedKey.base64).toEqual(wrappedKey.base64) | ||
expect(deserializedKey.config).toEqual(wrappedKey.config) | ||
expect(deserializedKey.vector!.asArray()).toEqual(wrappedKey.vector!.asArray()) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters