// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== namespace System.Security.Cryptography { /// /// Specifies the padding mode and parameters to use with RSA encryption or decryption operations. /// public sealed class RSAEncryptionPadding : IEquatable { private static readonly RSAEncryptionPadding s_pkcs1 = new RSAEncryptionPadding(RSAEncryptionPaddingMode.Pkcs1, default(HashAlgorithmName)); private static readonly RSAEncryptionPadding s_oaepSHA1 = CreateOaep(HashAlgorithmName.SHA1); private static readonly RSAEncryptionPadding s_oaepSHA256 = CreateOaep(HashAlgorithmName.SHA256); private static readonly RSAEncryptionPadding s_oaepSHA384 = CreateOaep(HashAlgorithmName.SHA384); private static readonly RSAEncryptionPadding s_oaepSHA512 = CreateOaep(HashAlgorithmName.SHA512); /// /// mode. /// public static RSAEncryptionPadding Pkcs1 { get { return s_pkcs1; } } /// /// mode with SHA1 hash algorithm. /// public static RSAEncryptionPadding OaepSHA1 { get { return s_oaepSHA1; } } /// /// mode with SHA256 hash algorithm. /// public static RSAEncryptionPadding OaepSHA256 { get { return s_oaepSHA256; } } /// /// mode with SHA384 hash algorithm. /// public static RSAEncryptionPadding OaepSHA384 { get { return s_oaepSHA384; } } /// /// mode with SHA512 hash algorithm. /// public static RSAEncryptionPadding OaepSHA512 { get { return s_oaepSHA512; } } private RSAEncryptionPaddingMode _mode; private HashAlgorithmName _oaepHashAlgorithm; private RSAEncryptionPadding(RSAEncryptionPaddingMode mode, HashAlgorithmName oaepHashAlgorithm) { _mode = mode; _oaepHashAlgorithm = oaepHashAlgorithm; } /// /// Creates a new instance instance representing /// with the given hash algorithm. /// public static RSAEncryptionPadding CreateOaep(HashAlgorithmName hashAlgorithm) { if (String.IsNullOrEmpty(hashAlgorithm.Name)) { throw new ArgumentException(Environment.GetResourceString("Cryptography_HashAlgorithmNameNullOrEmpty"), "hashAlgorithm"); } return new RSAEncryptionPadding(RSAEncryptionPaddingMode.Oaep, hashAlgorithm); } /// /// Gets the padding mode to use. /// public RSAEncryptionPaddingMode Mode { get { return _mode; } } /// /// Gets the padding mode to use in conjunction with . /// /// /// If is not , then will be null. /// public HashAlgorithmName OaepHashAlgorithm { get { return _oaepHashAlgorithm; } } public override int GetHashCode() { return CombineHashCodes(_mode.GetHashCode(), _oaepHashAlgorithm.GetHashCode()); } // Same as non-public Tuple.CombineHashCodes private static int CombineHashCodes(int h1, int h2) { return (((h1 << 5) + h1) ^ h2); } public override bool Equals(object obj) { return Equals(obj as RSAEncryptionPadding); } public bool Equals(RSAEncryptionPadding other) { return other != null && _mode == other._mode && _oaepHashAlgorithm == other._oaepHashAlgorithm; } public static bool operator ==(RSAEncryptionPadding left, RSAEncryptionPadding right) { if (Object.ReferenceEquals(left, null)) { return Object.ReferenceEquals(right, null); } return left.Equals(right); } public static bool operator !=(RSAEncryptionPadding left, RSAEncryptionPadding right) { return !(left == right); } public override string ToString() { return _mode.ToString() + _oaepHashAlgorithm.Name; } } }