@@ -26,11 +26,33 @@ namespace Mono.Cecil {
26
26
27
27
static class CryptoService {
28
28
29
- public static void StrongName ( Stream stream , ImageWriter writer , StrongNameKeyPair key_pair )
29
+ public static byte [ ] GetPublicKey ( WriterParameters parameters )
30
+ {
31
+ using ( var rsa = parameters . CreateRSA ( ) ) {
32
+ var cspBlob = CryptoConvert . ToCapiPublicKeyBlob ( rsa ) ;
33
+ var publicKey = new byte [ 12 + cspBlob . Length ] ;
34
+ Buffer . BlockCopy ( cspBlob , 0 , publicKey , 12 , cspBlob . Length ) ;
35
+ // The first 12 bytes are documented at:
36
+ // http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp
37
+ // ALG_ID - Signature
38
+ publicKey [ 1 ] = 36 ;
39
+ // ALG_ID - Hash
40
+ publicKey [ 4 ] = 4 ;
41
+ publicKey [ 5 ] = 128 ;
42
+ // Length of Public Key (in bytes)
43
+ publicKey [ 8 ] = ( byte ) ( cspBlob . Length >> 0 ) ;
44
+ publicKey [ 9 ] = ( byte ) ( cspBlob . Length >> 8 ) ;
45
+ publicKey [ 10 ] = ( byte ) ( cspBlob . Length >> 16 ) ;
46
+ publicKey [ 11 ] = ( byte ) ( cspBlob . Length >> 24 ) ;
47
+ return publicKey ;
48
+ }
49
+ }
50
+
51
+ public static void StrongName ( Stream stream , ImageWriter writer , WriterParameters parameters )
30
52
{
31
53
int strong_name_pointer ;
32
54
33
- var strong_name = CreateStrongName ( key_pair , HashStream ( stream , writer , out strong_name_pointer ) ) ;
55
+ var strong_name = CreateStrongName ( parameters , HashStream ( stream , writer , out strong_name_pointer ) ) ;
34
56
PatchStrongName ( stream , strong_name_pointer , strong_name ) ;
35
57
}
36
58
@@ -40,11 +62,11 @@ static void PatchStrongName (Stream stream, int strong_name_pointer, byte [] str
40
62
stream . Write ( strong_name , 0 , strong_name . Length ) ;
41
63
}
42
64
43
- static byte [ ] CreateStrongName ( StrongNameKeyPair key_pair , byte [ ] hash )
65
+ static byte [ ] CreateStrongName ( WriterParameters parameters , byte [ ] hash )
44
66
{
45
67
const string hash_algo = "SHA1" ;
46
68
47
- using ( var rsa = key_pair . CreateRSA ( ) ) {
69
+ using ( var rsa = parameters . CreateRSA ( ) ) {
48
70
var formatter = new RSAPKCS1SignatureFormatter ( rsa ) ;
49
71
formatter . SetHashAlgorithm ( hash_algo ) ;
50
72
@@ -74,7 +96,6 @@ static byte [] HashStream (Stream stream, ImageWriter writer, out int strong_nam
74
96
var sha1 = new SHA1Managed ( ) ;
75
97
var buffer = new byte [ buffer_size ] ;
76
98
using ( var crypto_stream = new CryptoStream ( Stream . Null , sha1 , CryptoStreamMode . Write ) ) {
77
-
78
99
stream . Seek ( 0 , SeekOrigin . Begin ) ;
79
100
CopyStreamChunk ( stream , crypto_stream , buffer , header_size ) ;
80
101
@@ -148,12 +169,17 @@ public static Guid ComputeGuid (byte [] hash)
148
169
149
170
static partial class Mixin {
150
171
151
- public static RSA CreateRSA ( this StrongNameKeyPair key_pair )
172
+ public static RSA CreateRSA ( this WriterParameters writer_parameters )
152
173
{
153
174
byte [ ] key ;
154
175
string key_container ;
155
176
156
- if ( ! TryGetKeyContainer ( key_pair , out key , out key_container ) )
177
+ if ( writer_parameters . StrongNameKeyBlob != null )
178
+ return CryptoConvert . FromCapiKeyBlob ( writer_parameters . StrongNameKeyBlob ) ;
179
+
180
+ if ( writer_parameters . StrongNameKeyContainer != null )
181
+ key_container = writer_parameters . StrongNameKeyContainer ;
182
+ else if ( ! TryGetKeyContainer ( writer_parameters . StrongNameKeyPair , out key , out key_container ) )
157
183
return CryptoConvert . FromCapiKeyBlob ( key ) ;
158
184
159
185
var parameters = new CspParameters {
0 commit comments