1
1
import { serialize , type , validate } from "@js-soft/ts-serval" ;
2
- import { Provider , SecurityLevel } from "@nmshd/rs-crypto-types" ;
3
2
import { CoreBuffer , Encoding , ICoreBuffer } from "./CoreBuffer" ;
4
- import { CryptoError } from "./CryptoError" ;
5
- import { CryptoErrorCode } from "./CryptoErrorCode" ;
6
- import { CryptoLayerKeyPair } from "./CryptoLayerKeyPair" ;
7
- import { getProvider } from "./CryptoLayerProviders" ;
8
3
import { CryptoSerializable } from "./CryptoSerializable" ;
9
4
import { CryptoExchangeAlgorithm } from "./exchange/CryptoExchange" ;
10
- import { CryptoHashAlgorithm } from "./hash/CryptoHash" ;
11
5
import { CryptoSignatureAlgorithm } from "./signature/CryptoSignatureAlgorithm" ;
12
6
13
7
export interface ICryptoPrivateKey {
14
- privateKey : ICoreBuffer | CryptoLayerKeyPair ;
8
+ privateKey : ICoreBuffer ;
15
9
algorithm : CryptoExchangeAlgorithm | CryptoSignatureAlgorithm ;
16
10
toString ( ) : string ;
17
11
toPEM ( ) : string ;
@@ -36,36 +30,14 @@ export class CryptoPrivateKey extends CryptoSerializable implements ICryptoPriva
36
30
37
31
@validate ( )
38
32
@serialize ( )
39
- public privateKey : CoreBuffer | CryptoLayerKeyPair ;
33
+ public privateKey : CoreBuffer ;
40
34
41
35
public toPEM ( ) : string {
42
- if ( this . privateKey instanceof CoreBuffer ) {
43
- return this . privateKey . toString ( Encoding . Pem , "PRIVATE KEY" ) ;
44
- }
45
-
46
- if ( ! this . privateKey . keyPairHandle ) {
47
- throw new CryptoError (
48
- CryptoErrorCode . CalUninitializedKey ,
49
- "The key pair does not hold a key pair handle. It needs to be loaded from a provider via the init method."
50
- ) ;
51
- }
52
- let privateRawKey = this . privateKey . keyPairHandle . extractKey ( ) ;
53
- return new CoreBuffer ( privateRawKey ) . toString ( Encoding . Pem , "PRIVATE KEY" ) ;
36
+ return this . privateKey . toString ( Encoding . Pem , "PRIVATE KEY" ) ;
54
37
}
55
38
56
39
public override toString ( ) : string {
57
- if ( this . privateKey instanceof CoreBuffer ) {
58
- return this . privateKey . toString ( Encoding . Base64_UrlSafe_NoPadding ) ;
59
- }
60
-
61
- if ( ! this . privateKey . keyPairHandle ) {
62
- throw new CryptoError (
63
- CryptoErrorCode . CalUninitializedKey ,
64
- "The key pair does not hold a key pair handle. It needs to be loaded from a provider via the init method."
65
- ) ;
66
- }
67
- let privateRawKey = this . privateKey . keyPairHandle . extractKey ( ) ;
68
- return new CoreBuffer ( privateRawKey ) . toString ( Encoding . Base64_UrlSafe_NoPadding ) ;
40
+ return this . privateKey . toString ( Encoding . Base64_UrlSafe_NoPadding ) ;
69
41
}
70
42
71
43
protected static stripPEM ( pem : string ) : string {
@@ -80,62 +52,34 @@ export class CryptoPrivateKey extends CryptoSerializable implements ICryptoPriva
80
52
public static fromString (
81
53
value : string ,
82
54
algorithm : CryptoExchangeAlgorithm | CryptoSignatureAlgorithm ,
83
- encoding : Encoding = Encoding . Base64_UrlSafe_NoPadding ,
84
- provider : string | SecurityLevel | undefined = "Software" ,
85
- hashAlgorithm ?: CryptoHashAlgorithm
55
+ encoding : Encoding = Encoding . Base64_UrlSafe_NoPadding
86
56
) : CryptoPrivateKey {
87
57
const buffer : CoreBuffer = CoreBuffer . fromString ( value , encoding ) ;
88
- let providerInitalized = getProvider ( provider ) ;
89
- if ( ! providerInitalized ) {
90
- return this . fromAny ( { algorithm, privateKey : buffer } ) ;
91
- }
92
-
93
- // provider load from global with config
94
-
95
- return this . fromAny ( {
96
- algorithm,
97
- privateKey : CryptoLayerKeyPair . fromPrivateBufferWithAlgorithm (
98
- providerInitalized ,
99
- buffer ,
100
- algorithm ,
101
- hashAlgorithm
102
- )
103
- } ) ;
58
+ return this . fromAny ( { algorithm, privateKey : buffer } ) ;
104
59
}
105
60
106
61
public static fromObject (
107
62
value : any ,
108
- algorithm : CryptoExchangeAlgorithm | CryptoSignatureAlgorithm ,
109
- provider ?: Provider
63
+ algorithm : CryptoExchangeAlgorithm | CryptoSignatureAlgorithm
110
64
) : CryptoPrivateKey {
111
- const buffer : CoreBuffer = CoreBuffer . fromObject ( value ) ;
112
- if ( ! provider ) {
113
- return this . fromAny ( { algorithm, privateKey : buffer } ) ;
114
- }
115
- return this . fromAny ( {
116
- algorithm,
117
- privateKey : CryptoLayerKeyPair . fromPrivateBufferWithAlgorithm ( provider , buffer , algorithm )
118
- } ) ;
65
+ const buffer : ICoreBuffer = CoreBuffer . fromObject ( value ) ;
66
+
67
+ return this . fromAny ( { algorithm, privateKey : buffer } ) ;
119
68
}
120
69
121
70
public static fromPEM (
122
71
pem : string ,
123
- algorithm : CryptoExchangeAlgorithm | CryptoSignatureAlgorithm ,
124
- provider ?: Provider
72
+ algorithm : CryptoExchangeAlgorithm | CryptoSignatureAlgorithm
125
73
) : CryptoPrivateKey {
126
74
const value = this . stripPEM ( pem ) ;
127
- return this . fromString ( value , algorithm , Encoding . Base64 , provider ) ;
75
+ return this . fromString ( value , algorithm , Encoding . Base64 ) ;
128
76
}
129
77
130
78
public static from ( value : any ) : CryptoPrivateKey {
131
79
return this . fromAny ( value ) ;
132
80
}
133
81
134
- public static fromBase64 ( value : string , provider ?: Provider ) : CryptoPrivateKey {
135
- let privateKey = this . deserialize ( CoreBuffer . base64_utf8 ( value ) ) ;
136
- if ( provider && privateKey . privateKey instanceof CryptoLayerKeyPair ) {
137
- privateKey . privateKey . init ( provider ) ;
138
- }
139
- return privateKey ;
82
+ public static fromBase64 ( value : string ) : CryptoPrivateKey {
83
+ return this . deserialize ( CoreBuffer . base64_utf8 ( value ) ) ;
140
84
}
141
85
}
0 commit comments