Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de92d28
commit c247b7f
Showing
41 changed files
with
2,155 additions
and
30 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
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,9 @@ | ||
export class KeyPairExtracted { | ||
public publicKey: string | ||
public privateKey: string | ||
|
||
constructor (publicKey: string, privateKey: string) { | ||
this.publicKey = publicKey | ||
this.privateKey = privateKey | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,54 @@ | ||
import {ArrayBufferToBase64, Base64ToArrayBuffer} from '@esentri/transformer-functions' | ||
import {KeyPairConfig, KeyPairConfigBuilder} from './config/KeyPairConfig' | ||
import {SymmetricKey} from './SymmetricKey' | ||
import {SymmetricKeyConfig} from './config/SymmetricKeyConfig' | ||
import {FilterPrivateKeyUsage} from './config/KeyUsage' | ||
|
||
export class PrivateKey { | ||
private readonly key: CryptoKey | ||
|
||
constructor (key: CryptoKey) { | ||
this.key = key | ||
} | ||
|
||
public extractKey (): Promise<string> { | ||
return window.crypto.subtle.exportKey('pkcs8', this.key).then(rawKey => { | ||
return ArrayBufferToBase64(rawKey) | ||
}) as Promise<string> | ||
} | ||
|
||
public keyConfig(): KeyPairConfig { | ||
return new KeyPairConfigBuilder() | ||
.keyUsage(this.key.usages) | ||
.keyAlgorithm(this.key.algorithm.name!) | ||
.extractable(this.key.extractable) | ||
.build() | ||
} | ||
|
||
public unwrapKey (base64: string, config: SymmetricKeyConfig = SymmetricKeyConfig.DEFAULT) | ||
: Promise<SymmetricKey> { | ||
return window.crypto.subtle.unwrapKey('raw', | ||
Base64ToArrayBuffer(base64), | ||
this.key, | ||
this.key.algorithm.name!, | ||
config.keyAlgorithm, | ||
config.extractable, | ||
config.keyUsage) | ||
.then((key: CryptoKey) => new SymmetricKey(key)) as Promise<SymmetricKey> | ||
} | ||
|
||
public static fromBase64 (base64: string, config = KeyPairConfig.DEFAULT): Promise<PrivateKey> { | ||
return new Promise<PrivateKey>((resolve, reject) => { | ||
(window.crypto.subtle.importKey('pkcs8', | ||
Base64ToArrayBuffer(base64), | ||
config.keyParams, | ||
config.extractable, | ||
FilterPrivateKeyUsage(config.keyUsage)) as Promise<CryptoKey>) | ||
.then(key => resolve(new PrivateKey(key))) | ||
.catch(error => { | ||
console.error('couldn\'t create a private key from base64: ', error) | ||
reject(error) | ||
}) | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,52 @@ | ||
import {ArrayBufferToBase64, Base64ToArrayBuffer} from '@esentri/transformer-functions' | ||
import {KeyPairConfig, KeyPairConfigBuilder} from './config/KeyPairConfig' | ||
import {SymmetricKey} from './SymmetricKey' | ||
import {WrappedKey} from './WrappedKey' | ||
import {FilterPublicKeyUsage} from './config/KeyUsage' | ||
|
||
export class PublicKey { | ||
private readonly key: CryptoKey | ||
|
||
constructor (key: CryptoKey) { | ||
this.key = key | ||
} | ||
|
||
public extractKey (): Promise<string> { | ||
return window.crypto.subtle.exportKey('spki', this.key).then(rawKey => { | ||
return ArrayBufferToBase64(rawKey) | ||
}) as Promise<string> | ||
} | ||
|
||
public keyConfig(): KeyPairConfig { | ||
return new KeyPairConfigBuilder() | ||
.keyUsage(this.key.usages) | ||
.keyAlgorithm(this.key.algorithm.name!) | ||
.extractable(this.key.extractable) | ||
.build() | ||
} | ||
|
||
public wrapKey (symmetricKey: SymmetricKey): Promise<WrappedKey> { | ||
return window.crypto.subtle | ||
.wrapKey('raw', | ||
symmetricKey['cryptoKey'], | ||
this.key, | ||
this.key.algorithm.name!) | ||
.then(rawKey => | ||
new WrappedKey(ArrayBufferToBase64(rawKey), symmetricKey.keyConfig())) as Promise<WrappedKey> | ||
} | ||
|
||
public static fromBase64 (hexString: string, config = KeyPairConfig.DEFAULT): Promise<PublicKey> { | ||
return new Promise<PublicKey>(((resolve, reject) => { | ||
(window.crypto.subtle.importKey('spki', | ||
Base64ToArrayBuffer(hexString), | ||
config.keyParams, | ||
config.extractable, | ||
FilterPublicKeyUsage(config.keyUsage)) as Promise<CryptoKey>) | ||
.then(key => resolve(new PublicKey(key))) | ||
.catch(error => { | ||
console.error('couldn\'t create a public key from base64: ', error) | ||
reject(error) | ||
}) | ||
})) | ||
} | ||
} |
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,11 @@ | ||
import {SymmetricKeyConfig} from './crypto-wrapper' | ||
|
||
export class WrappedKey { | ||
public readonly base64: string | ||
public readonly config: SymmetricKeyConfig | ||
|
||
constructor (base64: string, config: SymmetricKeyConfig) { | ||
this.base64 = base64 | ||
this.config = config | ||
} | ||
} |
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
Oops, something went wrong.