summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Security/Cryptography/PKCS1MaskGenerationMethod.cs
blob: d9dcabe58ba3f74c6090610e9c4b9ef9159d0254 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

// 

namespace System.Security.Cryptography {
[System.Runtime.InteropServices.ComVisible(true)]
    public class PKCS1MaskGenerationMethod : MaskGenerationMethod
    {
        private String HashNameValue;

        //
        // public constructors
        //
        
        public PKCS1MaskGenerationMethod() {
            HashNameValue = "SHA1";
        }

        //
        // public properties
        //

        public String HashName {
            get { return HashNameValue; }
            set { 
                HashNameValue = value;
                if (HashNameValue == null) {
                    HashNameValue = "SHA1";
                }
            }
        }

        //
        // public methods
        //

        public override byte[] GenerateMask(byte[] rgbSeed, int cbReturn)
        {
            HashAlgorithm hash = (HashAlgorithm) CryptoConfig.CreateFromName(HashNameValue);
            byte[] rgbCounter = new byte[4];
            byte[] rgbT = new byte[cbReturn];

            uint counter = 0;
            for (int ib=0; ib<rgbT.Length; ) {
                //  Increment counter -- up to 2^32 * sizeof(Hash)
                Utils.ConvertIntToByteArray(counter++, ref rgbCounter);
                hash.TransformBlock(rgbSeed, 0, rgbSeed.Length, rgbSeed, 0);
                hash.TransformFinalBlock(rgbCounter, 0, 4);
                byte[] _hash = hash.Hash;
                hash.Initialize();
                if (rgbT.Length - ib > _hash.Length) {
                    Buffer.BlockCopy(_hash, 0, rgbT, ib, _hash.Length);
                } else {
                    Buffer.BlockCopy(_hash, 0, rgbT, ib, rgbT.Length - ib);
                }
                ib += hash.Hash.Length;
            }
            return rgbT;
        }
    }
}