Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / security / cryptography / dsasignatureformatter.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>Microsoft</OWNER>
7 // 
8
9 //
10 // DSASignatureFormatter.cs
11 //
12
13 using System;
14 using System.Diagnostics.Contracts;
15 using System.Security.Cryptography.X509Certificates;
16
17 namespace System.Security.Cryptography {
18     [System.Runtime.InteropServices.ComVisible(true)]
19     public class DSASignatureFormatter : AsymmetricSignatureFormatter {
20         DSA    _dsaKey;
21         String _oid;
22
23         //
24         // public constructors
25         //
26
27         public DSASignatureFormatter() {
28             // The hash algorithm is always SHA1
29             _oid = CryptoConfig.MapNameToOID("SHA1", OidGroup.HashAlgorithm);
30         }
31
32         public DSASignatureFormatter(AsymmetricAlgorithm key) : this() {
33             if (key == null) 
34                 throw new ArgumentNullException("key");
35             Contract.EndContractBlock();
36             _dsaKey = (DSA) key;
37         }
38
39         //
40         // public methods
41         //
42
43         public override void SetKey(AsymmetricAlgorithm key) {
44             if (key == null) 
45                 throw new ArgumentNullException("key");
46             Contract.EndContractBlock();
47             _dsaKey = (DSA) key;
48         }
49
50         public override void SetHashAlgorithm(String strName) {
51             if (CryptoConfig.MapNameToOID(strName, OidGroup.HashAlgorithm) != _oid)
52                 throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_InvalidOperation"));
53         }
54
55         public override byte[] CreateSignature(byte[] rgbHash) {
56             if (rgbHash == null)
57                 throw new ArgumentNullException("rgbHash");
58             Contract.EndContractBlock();
59
60             if (_oid == null)
61                 throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_MissingOID"));
62             if (_dsaKey == null)
63                 throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_MissingKey"));
64
65             return _dsaKey.CreateSignature(rgbHash);
66         }
67     }
68 }