Example #1
0
        /// <summary>
        /// 지정된 HashAlgorithm을 이용하는 HashEncryptor를 생성합니다.
        /// </summary>
        /// <param name="algorithm"></param>
        public HashEncryptor(HashAlgorithm algorithm) {
            algorithm.ShouldNotBeNull("algorithm");
            Algorithm = algorithm;

            if(IsDebugEnabled)
                log.Debug("Hash 계산을 위해 HashEncryptor를 생성합니다... Algorithm=[{0}]", algorithm.GetType().FullName);
        }
Example #2
0
        public static string Get(HashAlgorithm algo)
        {
            if (algo is SHA1)
                return "SHA1";
            if (algo is SHA256)
                return "SHA256";
            if (algo is SHA384)
                return "SHA384";
            if (algo is SHA512)
                return "SHA512";
            if (algo is MD5)
                return "MD5";
            if (algo is RIPEMD160)
                return "RIPEMD160";

            throw new UnknownAlgorithmException("uncatched algorithm " + algo.GetType());
        }
Example #3
0
        // Map a HashAlgorithm object to our HASH_ALGORITHM enumeration
        HASH_ALGORITHM map_hash_algorithm(HashAlgorithm hasher)
        {
            Type t = hasher.GetType();

            if (Object.ReferenceEquals(t, typeof(MD5CryptoServiceProvider)))
                return HASH_ALGORITHM.RSA_MD5;
            if (Object.ReferenceEquals(t, typeof(SHA1CryptoServiceProvider)))
                return HASH_ALGORITHM.RSA_SHA1;
            throw new ArgumentException("unknown HashAlgorithm");
        }
Example #4
0
        /// <summary>
        ///     Get the hash value of the assembly when hashed with a specific algorithm.  The actual hash
        ///     algorithm object is not used, however the same type of object will be used.
        /// </summary>
        public byte[] GenerateHash(HashAlgorithm hashAlg)
        {
            if (hashAlg == null)
                throw new ArgumentNullException("hashAlg");
            Contract.EndContractBlock();

            byte[] hashValue = GenerateHash(hashAlg.GetType());

            byte[] returnHash = new byte[hashValue.Length];
            Array.Copy(hashValue, returnHash, returnHash.Length);
            return returnHash;
        }
 public byte[] GenerateHash(HashAlgorithm hashAlg)
 {
     if (hashAlg == null)
     {
         throw new ArgumentNullException("hashAlg");
     }
     byte[] sourceArray = this.GenerateHash(hashAlg.GetType());
     byte[] destinationArray = new byte[sourceArray.Length];
     Array.Copy(sourceArray, destinationArray, destinationArray.Length);
     return destinationArray;
 }
        static HashResult Hash(byte[] text, byte[] salt, HashAlgorithm algorithm)
        {
            if (text == null || text.Length == 0)
                return null;
            if (salt == null || salt.Length < SALT_LENGTH)
                throw new ArgumentException("Must be atleast " + SALT_LENGTH.ToString() + " characters in length", "salt");

            // Het hashingalgoritme verwacht bytes. Converteer onze string naar bytes
            var bytesUnhashed = text.Concat(salt).ToArray();

            // Magic time
            var hash = algorithm.ComputeHash(bytesUnhashed);

            // Transformeer de salt en de hash naar een Base-64 encoded string zodat je deze gemakkelijk kan opslaan en opsturen
            return new HashResult(algorithm.GetType().Name, Convert.ToBase64String(salt), Convert.ToBase64String(hash));
        }
Example #7
0
        private static void TestHash(HashAlgorithm hashAlgorithm, int count, int size)
        {
            // Create test string of relevant length.
            Random random = new Random();

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < count; i++)
            {
                StringBuilder sb = new StringBuilder();
                for (int a = 0; a < size; a++)
                    sb.Append(CharacterPool[(int) (random.NextDouble() * CharacterPool.Length)]);

                string testString = sb.ToString();
                GetHash(hashAlgorithm, testString);
            }
            stopwatch.Stop();
            Trace.WriteLine(
                string.Format(
                    "{1}ms\t{2} chars x {3}\t{0}",
                    hashAlgorithm.GetType(),
                    stopwatch.ElapsedTicks * 1000 / Stopwatch.Frequency,
                    size,
                    count));
        }
 internal static void LogVerifySignedInfo(SignedXml signedXml, AsymmetricAlgorithm key, SignatureDescription signatureDescription, HashAlgorithm hashAlgorithm, AsymmetricSignatureDeformatter asymmetricSignatureDeformatter, byte[] actualHashValue, byte[] signatureValue)
 {
     if (InformationLoggingEnabled)
     {
         string data = string.Format(CultureInfo.InvariantCulture, SecurityResources.GetResourceString("Log_VerifySignedInfoAsymmetric"), new object[] { GetKeyName(key), signatureDescription.GetType().Name, hashAlgorithm.GetType().Name, asymmetricSignatureDeformatter.GetType().Name });
         WriteLine(signedXml, TraceEventType.Information, SignedXmlDebugEvent.VerifySignedInfo, data);
     }
     if (VerboseLoggingEnabled)
     {
         string str2 = string.Format(CultureInfo.InvariantCulture, SecurityResources.GetResourceString("Log_ActualHashValue"), new object[] { FormatBytes(actualHashValue) });
         WriteLine(signedXml, TraceEventType.Verbose, SignedXmlDebugEvent.VerifySignedInfo, str2);
         string str3 = string.Format(CultureInfo.InvariantCulture, SecurityResources.GetResourceString("Log_RawSignatureValue"), new object[] { FormatBytes(signatureValue) });
         WriteLine(signedXml, TraceEventType.Verbose, SignedXmlDebugEvent.VerifySignedInfo, str3);
     }
 }
 internal static void LogSigning(SignedXml signedXml, object key, SignatureDescription signatureDescription, HashAlgorithm hash, AsymmetricSignatureFormatter asymmetricSignatureFormatter)
 {
     if (InformationLoggingEnabled)
     {
         string data = string.Format(CultureInfo.InvariantCulture, SecurityResources.GetResourceString("Log_SigningAsymmetric"), new object[] { GetKeyName(key), signatureDescription.GetType().Name, hash.GetType().Name, asymmetricSignatureFormatter.GetType().Name });
         WriteLine(signedXml, TraceEventType.Information, SignedXmlDebugEvent.Signing, data);
     }
 }