Skip to content

Commit

Permalink
feat: implemented import and export methods for raw public key in `Cr…
Browse files Browse the repository at this point in the history
…yptoPublicKeyHandle`
  • Loading branch information
WyvernIXTL committed Feb 19, 2025
1 parent 58cdb80 commit bfce95e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/crypto-layer/CryptoPrivateKeyHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export interface ICryptoPrivateKeyHandle {

export interface ICryptoPrivateKeyHandleStatic {
new (): ICryptoPrivateKeyHandle;
fromPEM(providerIdent: ProviderIdentifier, pem: string, config: KeyPairSpec): Promise<ICryptoPrivateKeyHandle>;
fromPEM(providerIdent: ProviderIdentifier, pem: string, spec: KeyPairSpec): Promise<ICryptoPrivateKeyHandle>;
fromString(
providerIdent: ProviderIdentifier,
value: string,
config: KeyPairSpec,
spec: KeyPairSpec,
encoding: Encoding
): Promise<ICryptoPrivateKeyHandle>;
// fromNativeKey(providerIdent: ProviderIdentifier, key: any, config: KeyPairSpec): Promise<ICryptoPrivateKeyHandle>;
Expand All @@ -38,23 +38,23 @@ export class CryptoPrivateKeyHandle extends CryptoAsymmetricKeyHandle implements
public static async fromString(
providerIdent: ProviderIdentifier,
value: string,
config: KeyPairSpec,
spec: 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);
const keyPairHandle = await provider.importKeyPair(spec, new Uint8Array(0), raw);
return await CryptoPrivateKeyHandle.newFromProviderAndKeyPairHandle(provider, keyPairHandle, {
keySpec: config
keySpec: spec
});
}

public static async fromPEM(
providerIdent: ProviderIdentifier,
pem: string,
config: KeyPairSpec
spec: KeyPairSpec
): Promise<CryptoPrivateKeyHandle> {
return await CryptoPrivateKeyHandle.fromString(providerIdent, pem, config, Encoding.Pem);
return await CryptoPrivateKeyHandle.fromString(providerIdent, pem, spec, Encoding.Pem);
}
}

Expand Down
48 changes: 46 additions & 2 deletions src/crypto-layer/CryptoPublicKeyHandle.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,60 @@
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 ICryptoPublicKeyHandle {
keyPairHandle: KeyPairHandle;
spec: KeyPairSpec;
toSerializedString(): Promise<string>;
toPEM(): Promise<string>;
toJSON(): Object;
}

export interface ICryptoPublicKeyHandleStatic {
new (): ICryptoPublicKeyHandle;
fromNativeKey(key: any, spec: KeyPairSpec): Promise<ICryptoPublicKeyHandle>;
fromPEM(providerIdent: ProviderIdentifier, pem: string, spec: KeyPairSpec): Promise<CryptoPublicKeyHandle>;
fromString(
providerIdent: ProviderIdentifier,
value: string,
spec: KeyPairSpec,
encoding: Encoding
): Promise<CryptoPublicKeyHandle>;
// fromNativeKey(key: any, spec: KeyPairSpec): Promise<ICryptoPublicKeyHandle>;
}

@type("CryptoPublicKeyHandle")
export class CryptoPublicKeyHandle extends CryptoAsymmetricKeyHandle implements ICryptoPublicKeyHandle {}
export class CryptoPublicKeyHandle extends CryptoAsymmetricKeyHandle implements ICryptoPublicKeyHandle {
public async toSerializedString(): Promise<string> {
const raw = await this.keyPairHandle.getPublicKey();
return CoreBuffer.from(raw).toString(Encoding.Base64_UrlSafe_NoPadding);
}

public async toPEM(): Promise<string> {
const raw = await this.keyPairHandle.getPublicKey();
return CoreBuffer.from(raw).toString(Encoding.Pem, "PRIVATE KEY");
}

public static async fromString(
providerIdent: ProviderIdentifier,
value: string,
spec: KeyPairSpec,
encoding: Encoding
): Promise<CryptoPublicKeyHandle> {
const raw = CoreBuffer.fromString(value, encoding).buffer;
const provider = getProviderOrThrow(providerIdent);
const keyPairHandle = await provider.importPublicKey(spec, raw);
return await CryptoPublicKeyHandle.newFromProviderAndKeyPairHandle(provider, keyPairHandle, {
keySpec: spec
});
}

public static async fromPEM(
providerIdent: ProviderIdentifier,
pem: string,
spec: KeyPairSpec
): Promise<CryptoPublicKeyHandle> {
return await CryptoPublicKeyHandle.fromString(providerIdent, pem, spec, Encoding.Pem);
}
}

0 comments on commit bfce95e

Please sign in to comment.