Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / security / cryptography / hmacsha512.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>Microsoft</OWNER>
7 // 
8
9 //
10 // HMACSHA512.cs
11 //
12
13 namespace System.Security.Cryptography {
14     [System.Runtime.InteropServices.ComVisible(true)]
15     public class HMACSHA512 : HMAC {
16
17         private bool m_useLegacyBlockSize = Utils._ProduceLegacyHmacValues();
18
19         //
20         // public constructors
21         //
22
23         public HMACSHA512 () : this (Utils.GenerateRandom(128)) {}
24
25         [System.Security.SecuritySafeCritical]  // auto-generated
26         public HMACSHA512 (byte[] key) {
27             m_hashName = "SHA512";
28 #if FULL_AOT_RUNTIME
29             m_hash1 = new SHA512Managed();
30             m_hash2 = new SHA512Managed();
31 #else
32             m_hash1 = GetHashAlgorithmWithFipsFallback(() => new SHA512Managed(), () => HashAlgorithm.Create("System.Security.Cryptography.SHA512CryptoServiceProvider"));
33             m_hash2 = GetHashAlgorithmWithFipsFallback(() => new SHA512Managed(), () => HashAlgorithm.Create("System.Security.Cryptography.SHA512CryptoServiceProvider"));
34 #endif
35             HashSizeValue = 512;
36             BlockSizeValue = BlockSize;
37             base.InitializeKey(key);
38         }
39
40         private int BlockSize {
41             get { return m_useLegacyBlockSize ? 64 : 128; }
42         }
43
44         /// <summary>
45         ///     In Whidbey we incorrectly used a block size of 64 bytes for HMAC-SHA-384 and HMAC-SHA-512,
46         ///     rather than using the correct value of 128 bytes.  Setting this to true causes us to fall
47         ///     back to the Whidbey mode which produces incorrect HMAC values.
48         ///     
49         ///     This value should be set only once, before hashing has begun, since we need to reset the key
50         ///     buffer for the block size change to take effect.
51         ///     
52         ///     The default vaue is off, however this can be toggled for the application by setting the
53         ///     legacyHMACMode config switch.
54         /// </summary>
55         public bool ProduceLegacyHmacValues {
56             get { return m_useLegacyBlockSize; }
57
58             set {
59                 m_useLegacyBlockSize = value;
60
61                 BlockSizeValue = BlockSize;
62                 InitializeKey(KeyValue);
63             }
64         }
65     }
66 }