Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / security / cryptography / pkcs1maskgenerationmethod.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>Microsoft</OWNER>
7 // 
8
9 namespace System.Security.Cryptography {
10 [System.Runtime.InteropServices.ComVisible(true)]
11     public class PKCS1MaskGenerationMethod : MaskGenerationMethod
12     {
13         private String HashNameValue;
14
15         //
16         // public constructors
17         //
18         
19         public PKCS1MaskGenerationMethod() {
20             HashNameValue = "SHA1";
21         }
22
23         //
24         // public properties
25         //
26
27         public String HashName {
28             get { return HashNameValue; }
29             set { 
30                 HashNameValue = value;
31                 if (HashNameValue == null) {
32                     HashNameValue = "SHA1";
33                 }
34             }
35         }
36
37         //
38         // public methods
39         //
40
41         public override byte[] GenerateMask(byte[] rgbSeed, int cbReturn)
42         {
43 #if MONO
44             HashAlgorithm hash = HashAlgorithm.Create (HashNameValue);
45             return Mono.Security.Cryptography.PKCS1.MGF1 (hash, rgbSeed, cbReturn);
46 #else
47             HashAlgorithm hash = (HashAlgorithm) CryptoConfig.CreateFromName(HashNameValue);
48             byte[] rgbCounter = new byte[4];
49             byte[] rgbT = new byte[cbReturn];
50
51             uint counter = 0;
52             for (int ib=0; ib<rgbT.Length; ) {
53                 //  Increment counter -- up to 2^32 * sizeof(Hash)
54                 Utils.ConvertIntToByteArray(counter++, ref rgbCounter);
55                 hash.TransformBlock(rgbSeed, 0, rgbSeed.Length, rgbSeed, 0);
56                 hash.TransformFinalBlock(rgbCounter, 0, 4);
57                 byte[] _hash = hash.Hash;
58                 hash.Initialize();
59                 if (rgbT.Length - ib > _hash.Length) {
60                     Buffer.BlockCopy(_hash, 0, rgbT, ib, _hash.Length);
61                 } else {
62                     Buffer.BlockCopy(_hash, 0, rgbT, ib, rgbT.Length - ib);
63                 }
64                 ib += hash.Hash.Length;
65             }
66             return rgbT;
67 #endif
68         }
69     }
70 }