Skip to content

Commit

Permalink
v0.6.4: minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Maurice-Mueller committed Apr 30, 2019
1 parent 89e5138 commit 4f7e2e6
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@esentri/js-crypto-wrapper",
"version": "0.6.3",
"version": "0.6.4",
"description": "a wrapper around WebCryptoAPI",
"keywords": [],
"main": "dist/crypto-wrapper.umd.js",
Expand Down
5 changes: 3 additions & 2 deletions src/SymmetricKey.ts
Expand Up @@ -96,12 +96,13 @@ export class SymmetricKey {
.then((key: CryptoKey) => new PublicKey(key)) as Promise<PublicKey>
}

public unwrapPrivateKey (base64: string, vector: InitializationVector, config: KeyPairConfig = KeyPairConfig.DEFAULT)
public unwrapPrivateKey (base64: string, vector: InitializationVector | Uint8Array, config: KeyPairConfig = KeyPairConfig.DEFAULT)
: Promise<PrivateKey> {
let iv = (vector as any)['vector'] ? (vector as any)['vector'] : vector
return window.crypto.subtle.unwrapKey('pkcs8',
Base64WithBinaryDataToArrayBuffer(base64),
this.key,
this.decryptionParameters(vector),
this.decryptionParameters(iv),
config.keyParams,
config.extractable,
config.keyUsage)
Expand Down
15 changes: 15 additions & 0 deletions test/SymmetricKey.test.ts
Expand Up @@ -279,7 +279,22 @@ describe('test symmetric key', () => {
})
})
})
})

it('(un)wrap private key with Uint8Array initialization vector', done => {
PrivateKey.fromBase64(PrivateKeyBase64).then(privateKey => {
SymmetricKey.random().then(symKey => {
symKey.wrapKey(privateKey).then(wrappedKey => {
symKey.unwrapPrivateKey(wrappedKey.base64, (wrappedKey.vector! as any).vector)
.then(unwrappedPrivateKey => {
unwrappedPrivateKey.extractKey().then(extractedUnwrapped => {
expect(extractedUnwrapped).toEqual(PrivateKeyBase64)
done()
})
})
})
})
})
})

it('(un)wrap private key with sym key from password', done => {
Expand Down
12 changes: 12 additions & 0 deletions test/from_production/PublicKey.test.ts
@@ -0,0 +1,12 @@
import {PublicKey} from '../../src/crypto-wrapper'

describe('test public key with stuff from production', () => {

it('public key from base 64 from production', done => {
let base64 = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk9Qnk67zh6zSre5n7qx+55q1plaTKgwZMLo00gE6gTf4n1jJPBCmifIqdCAxSVP1bQK4LTcHwsERDki1LWsk76bGKna73PFxkSO6AqOlggPxaMHc5B/zVaY4zDfPokRfui6Cy4/2k2WtOwnk2Jr0dMt/x15521EP7MNmRreobfTKt69dshkbhmU3WNrIrV/qYqVYjqj1dsDdHL6Cj4Euv5uA31y1VY/cMuEtOoq/POQ+SNYddgIP3ThPvFmNF/cHdpqN/E9OsXrctCettTvPXhD6J8OCM9ft2dlQle/dcuz+RY7mVynts4+ksMnICT5aHsaRX5TSEaEjGbAUi3WZUwIDAQAB'
PublicKey.fromBase64(base64).then(_ => {
done()
})
})

})
45 changes: 45 additions & 0 deletions test/from_production/SymmetricKey.test.ts
@@ -0,0 +1,45 @@
import {SymmetricKey} from '../../src/SymmetricKey'
import {WebCryptoConfig} from '../../src/config/WebCryptoConfig'
import {extractable} from './../CryptoKeyAssertions'
import {SymmetricKeyConfigBuilder} from '../../src/config/SymmetricKeyConfig'
import {SymmetricKeyDerivationConfigBuilder} from '../../src/config/SymmetricKeyDerivationConfig'
import {KeyAlgorithm} from '../../src/config/KeyAlgorithm'
import {PrivateKeyBase64, publicKeyBase64, symKeyBase64} from './../testData/Keys'
import {
ArrayBufferWithBinaryDataToBase64, ArrayBufferWithBinaryDataToString, Base64WithBinaryDataToArrayBuffer
} from '@esentri/transformer-functions'
import {Array1000Byte, Array100Byte} from './../testData/ArraysForEncryption'
import {ArrayBufferEqual} from './../content/ArrayBufferFunctions'
import * as fs from 'fs'
import {InitializationVector, PrivateKey, PublicKey, WrappedKeyDeserialize, WrappedKeySerialize} from '../../src/crypto-wrapper'
import {test} from 'shelljs'

describe('test symmetric key with stuff from production', () => {

it('private key (un)wrapping with symmetric key and (de)serialization', done => {
PrivateKey.fromBase64(PrivateKeyBase64).then(privateKey => {
SymmetricKey.fromPassword('hello world').then(symKey => {
symKey.wrapKey(privateKey).then(wrappedPrivateKey => {
WrappedKeySerialize(wrappedPrivateKey).then(serializedWrappedPrivateKey => {
WrappedKeyDeserialize(serializedWrappedPrivateKey).then(deserializedWrappedPrivateKey => {
symKey.unwrapPrivateKey(
deserializedWrappedPrivateKey.base64,
deserializedWrappedPrivateKey.vector!!,
deserializedWrappedPrivateKey.config
).then(unwrappedPrivateKey => {
unwrappedPrivateKey.extractKey().then(extractedUnwrappedPrivateKey => {
privateKey.extractKey().then(extractedPrivateKey => {
expect(extractedUnwrappedPrivateKey).toEqual(extractedPrivateKey)
expect(unwrappedPrivateKey.keyConfig()).toEqual(privateKey.keyConfig())
done()
})
})
})
})
})
})
})
})
})

})

0 comments on commit 4f7e2e6

Please sign in to comment.