-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented methods for crypto private key handle to import and…
… export raw keys
- Loading branch information
1 parent
e2daf27
commit 4c036df
Showing
2 changed files
with
82 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,61 @@ | ||
import { type } from "@js-soft/ts-serval"; | ||
import { KeyPairHandle, KeyPairSpec } from "@nmshd/rs-crypto-types"; | ||
import { CoreBuffer, Encoding } from "src/CoreBuffer"; | ||
import { CryptoAsymmetricKeyHandle } from "./CryptoAsymmetricKeyHandle"; | ||
import { getProviderOrThrow, ProviderIdentifier } from "./CryptoLayerProviders"; | ||
|
||
export interface ICryptoPrivateKeyHandle { | ||
keyPairHandle: KeyPairHandle; | ||
spec: KeyPairSpec; | ||
toSerializedString(): Promise<string>; | ||
toPEM(): Promise<string>; | ||
} | ||
|
||
export interface ICryptoPrivateKeyHandleStatic { | ||
new (): ICryptoPrivateKeyHandle; | ||
fromNativeKey(key: any, spec: KeyPairSpec): Promise<ICryptoPrivateKeyHandle>; | ||
fromPEM(providerIdent: ProviderIdentifier, pem: string, config: KeyPairSpec): Promise<ICryptoPrivateKeyHandle>; | ||
fromString( | ||
providerIdent: ProviderIdentifier, | ||
value: string, | ||
config: KeyPairSpec, | ||
encoding: Encoding | ||
): Promise<ICryptoPrivateKeyHandle>; | ||
// fromNativeKey(providerIdent: ProviderIdentifier, key: any, config: KeyPairSpec): Promise<ICryptoPrivateKeyHandle>; | ||
} | ||
|
||
@type("CryptoPrivateKeyHandle") | ||
export class CryptoPrivateKeyHandle extends CryptoAsymmetricKeyHandle implements ICryptoPrivateKeyHandle {} | ||
export class CryptoPrivateKeyHandle extends CryptoAsymmetricKeyHandle implements ICryptoPrivateKeyHandle { | ||
public async toSerializedString(): Promise<string> { | ||
const raw = await this.keyPairHandle.extractKey(); | ||
return CoreBuffer.from(raw).toString(Encoding.Base64_UrlSafe_NoPadding); | ||
} | ||
|
||
public async toPEM(): Promise<string> { | ||
const raw = await this.keyPairHandle.extractKey(); | ||
return CoreBuffer.from(raw).toString(Encoding.Pem, "PRIVATE KEY"); | ||
} | ||
|
||
public static async fromString( | ||
providerIdent: ProviderIdentifier, | ||
value: string, | ||
config: KeyPairSpec, | ||
encoding: Encoding | ||
): Promise<CryptoPrivateKeyHandle> { | ||
const raw = CoreBuffer.fromString(value, encoding).buffer; | ||
const provider = getProviderOrThrow(providerIdent); | ||
const keyPairHandle = await provider.importKeyPair(config, new Uint8Array(0), raw); | ||
return await CryptoPrivateKeyHandle.newFromProviderAndKeyPairHandle(provider, keyPairHandle, { | ||
keySpec: config | ||
}); | ||
} | ||
|
||
public static async fromPEM( | ||
providerIdent: ProviderIdentifier, | ||
pem: string, | ||
config: KeyPairSpec | ||
): Promise<CryptoPrivateKeyHandle> { | ||
return await CryptoPrivateKeyHandle.fromString(providerIdent, pem, config, Encoding.Pem); | ||
} | ||
} | ||
|
||
const _testAssign: ICryptoPrivateKeyHandleStatic = CryptoPrivateKeyHandle; |