Skip to content

Commit

Permalink
feat: implemented methods for crypto private key handle to import and…
Browse files Browse the repository at this point in the history
… export raw keys
  • Loading branch information
WyvernIXTL committed Feb 19, 2025
1 parent e2daf27 commit 4c036df
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/crypto-layer/CryptoAsymmetricKeyHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,41 @@ export class CryptoAsymmetricKeyHandle extends CryptoSerializableAsync {

public keyPairHandle: KeyPairHandle;

protected static async newFromProviderAndKeyPairHandle<T extends CryptoAsymmetricKeyHandle>(
this: new () => T,
provider: Provider,
keyPairHandle: KeyPairHandle,
other?: {
providerName?: string;
keyId?: string;
keySpec?: KeyPairSpec;
}
): Promise<T> {
const result = new this();

if (other?.providerName) {
result.providerName = other.providerName;
} else {
result.providerName = await provider.providerName();
}

if (other?.keyId) {
result.id = other.keyId;
} else {
result.id = await keyPairHandle.id();
}

if (other?.keySpec) {
result.spec = other.keySpec;
} else {
result.spec = await keyPairHandle.spec();
}

result.provider = provider;
result.keyPairHandle = keyPairHandle;
return result;
}

public static async from(value: any): Promise<CryptoAsymmetricKeyHandle> {
return await this.fromAny(value);
}
Expand Down
49 changes: 47 additions & 2 deletions src/crypto-layer/CryptoPrivateKeyHandle.ts
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;

0 comments on commit 4c036df

Please sign in to comment.