@@ -28,22 +28,17 @@ public class NetMQCertificate
28
28
0x11 , 0x12 , 0x13 , 0x14 , 0x15 , 0x16 , 0x17 , 0x18 , 0x19 , 0x1A , 0x1B , 0x1C ,
29
29
0x1D , 0x1E , 0x1F , 0x20 , 0x21 , 0x22 , 0x23 , 0x4F , 0xFF , 0x50 , 0xFF , 0xFF } ;
30
30
31
- // --------------------------------------------------------------------------
32
- // Encode a binary frame as a string; destination string MUST be at least
33
- // size * 5 / 4 bytes long plus 1 byte for the null terminator. Returns
34
- // dest. Size must be a multiple of 4.
35
- // Returns NULL for invalid input.
31
+ /// <summary>
32
+ /// Encodes Z85 string
33
+ /// </summary>
34
+ /// <param name="data">Data</param>
36
35
private string Z85Encode ( byte [ ] data )
37
36
{
38
- if ( data . Length % 4 != 0 )
39
- {
40
- return null ;
41
- }
42
-
43
37
byte byte_nbr = 0 ;
44
38
UInt32 value = 0 ;
45
39
string dest = null ;
46
- while ( byte_nbr < data . Length ) {
40
+ while ( byte_nbr < data . Length )
41
+ {
47
42
// Accumulate value in base 256 (binary)
48
43
value = value * 256 + data [ byte_nbr ++ ] ;
49
44
if ( byte_nbr % 4 == 0 )
@@ -58,45 +53,41 @@ private string Z85Encode(byte[] data)
58
53
value = 0 ;
59
54
}
60
55
}
61
-
62
- dest += char . MinValue ;
63
56
return dest ;
64
57
}
65
58
66
-
67
- // --------------------------------------------------------------------------
68
- // Decode an encoded string into a binary frame; dest must be at least
69
- // strlen (string) * 4 / 5 bytes long. Returns dest. strlen (string)
70
- // must be a multiple of 5.
71
- // Returns NULL for invalid input.
59
+ /// <summary>
60
+ /// Decodes Z85 string
61
+ /// </summary>
62
+ /// <param name="key">key in Z85 format</param>
63
+ /// <exception cref="ArgumentException">If key in invalid</exception>
72
64
byte [ ] Z85Decode ( string key )
73
65
{
74
66
UInt32 byte_nbr = 0 ;
75
67
UInt32 char_nbr = 0 ;
76
68
UInt32 value = 0 ;
77
69
var dest_ = new List < byte > ( ) ;
78
- foreach ( var cha in key . TakeWhile ( c => c != char . MinValue ) )
70
+ foreach ( var cha in key )
79
71
{
80
-
81
72
// Accumulate value in base 85
82
73
if ( UInt32 . MaxValue / 85 < value )
83
74
{
84
75
// Invalid z85 encoding, represented value exceeds 0xffffffff
85
- return null ;
76
+ throw new ArgumentException ( "Invalid key bad encoding" ) ;
86
77
}
87
78
value *= 85 ;
88
79
char_nbr ++ ;
89
80
var index = cha - 32 ;
90
81
if ( index >= Decoder . Length )
91
82
{
92
83
// Invalid z85 encoding, character outside range
93
- return null ;
84
+ throw new ArgumentException ( "Invalid key character" ) ;
94
85
}
95
86
UInt32 summand = Decoder [ index ] ;
96
87
if ( summand == 0xFF || summand > ( UInt32 . MaxValue - value ) )
97
88
{
98
89
// Invalid z85 encoding, invalid character or represented value exceeds 0xffffffff
99
- return null ;
90
+ throw new ArgumentException ( "Invalid key character" ) ;
100
91
}
101
92
value += summand ;
102
93
if ( char_nbr % 5 == 0 )
@@ -111,12 +102,7 @@ byte[] Z85Decode(string key)
111
102
value = 0 ;
112
103
}
113
104
}
114
- if ( char_nbr % 5 != 0 )
115
- {
116
- return null ;
117
- }
118
105
return dest_ . ToArray ( ) ;
119
-
120
106
}
121
107
122
108
/// <summary>
@@ -152,14 +138,14 @@ public NetMQCertificate(byte[] secretKey, byte[] publicKey)
152
138
/// </summary>
153
139
/// <param name="secretKey">Secret key</param>
154
140
/// <param name="publicKey">Public key</param>
155
- /// <exception cref="ArgumentException">If secretKey or publicKey are not 41 -chars long</exception>
141
+ /// <exception cref="ArgumentException">If secretKey or publicKey are not 40 -chars long</exception>
156
142
public NetMQCertificate ( string secretKey , string publicKey )
157
143
{
158
- if ( secretKey . Length != 41 )
159
- throw new ArgumentException ( "secretKey must be 41 char long" ) ;
144
+ if ( secretKey . Length != 40 )
145
+ throw new ArgumentException ( "secretKey must be 40 char long" ) ;
160
146
161
- if ( publicKey . Length != 41 )
162
- throw new ArgumentException ( "publicKey must be 41 char long" ) ;
147
+ if ( publicKey . Length != 40 )
148
+ throw new ArgumentException ( "publicKey must be 40 char long" ) ;
163
149
164
150
SecretKey = Z85Decode ( secretKey ) ;
165
151
PublicKey = Z85Decode ( publicKey ) ;
@@ -181,8 +167,8 @@ private NetMQCertificate(byte[] key, bool isSecret)
181
167
182
168
private NetMQCertificate ( string keystr , bool isSecret )
183
169
{
184
- if ( keystr . Length != 41 )
185
- throw new ArgumentException ( "key must be 41 bytes length" ) ;
170
+ if ( keystr . Length != 40 )
171
+ throw new ArgumentException ( "key must be 40 bytes length" ) ;
186
172
187
173
var key = Z85Decode ( keystr ) ;
188
174
if ( isSecret )
@@ -209,7 +195,7 @@ public NetMQCertificate FromSecretKey(byte[] secretKey)
209
195
/// Create a certificate from secret key, public key is derived from the secret key
210
196
/// </summary>
211
197
/// <param name="secretKey">Secret Key</param>
212
- /// <exception cref="ArgumentException">If secret key is not 41 -chars long</exception>
198
+ /// <exception cref="ArgumentException">If secret key is not 40 -chars long</exception>
213
199
/// <returns>The newly created certificate</returns>
214
200
public NetMQCertificate FromSecretKey ( string secretKey )
215
201
{
@@ -231,7 +217,7 @@ public static NetMQCertificate FromPublicKey(byte[] publicKey)
231
217
/// Create a public key only certificate.
232
218
/// </summary>
233
219
/// <param name="publicKey">Public key</param>
234
- /// <exception cref="ArgumentException">If public key is not 41 -chars long</exception>
220
+ /// <exception cref="ArgumentException">If public key is not 40 -chars long</exception>
235
221
/// <returns>The newly created certificate</returns>
236
222
public static NetMQCertificate FromPublicKey ( string publicKey )
237
223
{
0 commit comments