コード例 #1
0
 private protected HashAlgorithm(string mechanism, KeySizes[] hashSizes, int hashSize)
 {
     if (!DetectionUtilities.ValidSize(hashSizes, hashSize))
     {
         throw new CryptographicException("Unsupported hash size.");
     }
     this.Mechanism = mechanism;
     this.HashSize  = hashSize;
 }
コード例 #2
0
 private protected BlockAlgorithm(string mechanism, KeySizes[] blockSizes, int blockSize, KeySizes[] keySizes) : base(mechanism)
 {
     if (!DetectionUtilities.ValidSize(blockSizes, blockSize))
     {
         throw new CryptographicException("Unsupported block size.");
     }
     this.BlockSize = blockSize;
     _keySizes      = keySizes;
 }
コード例 #3
0
        internal static bool TryGetNanoAlgorithm(string mechanism, out IHashAlgorithm algorithm)
        {
            string[] splits = mechanism.Split('-');
            if (splits.Length >= 2 && splits.Length <= 3)
            {
                mechanism = string.Empty;
                int hashSize  = 0;
                int stateSize = 0;
                switch (splits[0])
                {
                case "BLAKE2B":
                case "BLAKE2S":
                    if (int.TryParse(splits[1], out hashSize))
                    {
                        mechanism = splits[0];
                    }
                    break;

                case "SHA":
                    if (splits.Length == 3)
                    {
                        if (splits[1] == "512" && int.TryParse(splits[2], out hashSize))
                        {
                            mechanism = "SHA512T";
                        }
                    }
                    break;

                case "SHA512":
                    if (int.TryParse(splits[1], out hashSize))
                    {
                        mechanism = "SHA512T";
                    }
                    break;

                case "SKEIN":
                    if (splits.Length == 3)
                    {
                        if (int.TryParse(splits[1], out stateSize) && int.TryParse(splits[2], out hashSize))
                        {
                            mechanism = splits[0];
                        }
                    }
                    break;

                default: break;
                }
                if (mechanism.Length > 0)
                {
                    bool legal;
                    switch (mechanism)
                    {
                    case "BLAKE2B":
                    {
                        legal     = DetectionUtilities.ValidSize(BLAKE2b.HashSizes, hashSize);
                        algorithm = legal ? new BLAKE2b(hashSize) : null;
                        return(legal);
                    }

                    case "BLAKE2S":
                    {
                        legal     = DetectionUtilities.ValidSize(BLAKE2s.HashSizes, hashSize);
                        algorithm = legal ? new BLAKE2s(hashSize) : null;
                        return(legal);
                    }

                    case "SHA512T":
                    {
                        legal     = DetectionUtilities.ValidSize(SHA512T.HashSizes, hashSize);
                        algorithm = legal ? new SHA512T(hashSize) : null;
                        return(legal);
                    }

                    case "SKEIN":
                    {
                        legal     = DetectionUtilities.ValidSize(Skein.HashSizes, hashSize);
                        legal    &= DetectionUtilities.ValidSize(Skein.StateSizes, stateSize);
                        algorithm = legal ? new Skein(hashSize, stateSize) : null;
                        return(legal);
                    }

                    default: algorithm = null; return(false);
                    }
                }
            }
            algorithm = null;
            return(false);
        }