Esempio n. 1
0
 public static HashAlgorithmName GetHashAlgorithmName(TLSHashAlgorithm hashAlgorithm)
 {
     switch (hashAlgorithm)
     {
         case TLSHashAlgorithm.MD5: return HashAlgorithmName.MD5;
         case TLSHashAlgorithm.SHA1: return HashAlgorithmName.SHA1;
         case TLSHashAlgorithm.SHA256: return HashAlgorithmName.SHA256;
         case TLSHashAlgorithm.SHA384: return HashAlgorithmName.SHA384;
         case TLSHashAlgorithm.SHA512: return HashAlgorithmName.SHA512;
         default: throw new NotSupportedException();
     }
 }
Esempio n. 2
0
 public static int GetHashLen(TLSHashAlgorithm hashAlgorithm)
 {
     switch (hashAlgorithm)
     {
         case TLSHashAlgorithm.SHA1:
             return 160;
         case TLSHashAlgorithm.SHA256:
             return 256;
         case TLSHashAlgorithm.SHA384:
             return 384;
         case TLSHashAlgorithm.SHA512:
             return 512;
         default:
             throw new NotSupportedException();
     }
 }
Esempio n. 3
0
        public static int GetHashLen(TLSHashAlgorithm hashAlgorithm)
        {
            switch (hashAlgorithm)
            {
            case TLSHashAlgorithm.SHA1:
                return(160);

            case TLSHashAlgorithm.SHA256:
                return(256);

            case TLSHashAlgorithm.SHA384:
                return(384);

            case TLSHashAlgorithm.SHA512:
                return(512);

            default:
                throw new NotSupportedException();
            }
        }
        public static IServiceCollection RegisterCipherSuite(
            this IServiceCollection serviceCollection,
            CipherSuite suite,
            TLSCipherAlgorithm cipher,
            TLSHashAlgorithm digest,
            TLSSignatureAlgorithm signature,
            TLSKeyExchange exchange)
        {
            return(serviceCollection.Update <CipherSuitesRegistry>(prev =>
            {
                prev = prev ?? new CipherSuitesRegistry();

                prev.Register(
                    suite: suite,
                    cipher: cipher,
                    digest: digest,
                    signature: signature,
                    exchange: exchange);

                return prev;
            }));
        }
Esempio n. 5
0
        public static Hasher Create(TLSHashAlgorithm hashAlgorithm)
        {
            switch (hashAlgorithm)
            {
#if NET45 || NET451
                case TLSHashAlgorithm.MD5: return new HashAlgorithmHasher(new MD5CryptoServiceProvider());
                case TLSHashAlgorithm.SHA1: return new HashAlgorithmHasher(new SHA1CryptoServiceProvider());
                case TLSHashAlgorithm.SHA256: return new HashAlgorithmHasher(new SHA256CryptoServiceProvider());
                case TLSHashAlgorithm.SHA384: return new HashAlgorithmHasher(new SHA384CryptoServiceProvider());
                case TLSHashAlgorithm.SHA512: return new HashAlgorithmHasher(new SHA512CryptoServiceProvider());
                case TLSHashAlgorithm.MD5SHA1: return new HashAlgorithmHasher(new MD5SHA1());
                default: throw new NotSupportedException();
#else
                case TLSHashAlgorithm.MD5: return new IncrementalHashHasher(HashAlgorithmName.MD5);
                case TLSHashAlgorithm.SHA1: return new IncrementalHashHasher(HashAlgorithmName.SHA1);
                case TLSHashAlgorithm.SHA256: return new IncrementalHashHasher(HashAlgorithmName.SHA256);
                case TLSHashAlgorithm.SHA384: return new IncrementalHashHasher(HashAlgorithmName.SHA384);
                case TLSHashAlgorithm.SHA512: return new IncrementalHashHasher(HashAlgorithmName.SHA512);
                case TLSHashAlgorithm.MD5SHA1: return new MD5SHA1IncrementalHasher();
                default: throw new NotSupportedException();
#endif
            }
        }
        public static IServiceCollection RegisterHashAlgorithm <T>(this IServiceCollection serviceCollection, TLSHashAlgorithm hashAlgorithm)
            where T : class, IDigest, new()
        {
            return(serviceCollection.Update <HashAlgorithmRegistry>(prev =>
            {
                prev = prev ?? new HashAlgorithmRegistry();

                prev.Register(hashAlgorithm, () => new T());

                return prev;
            }));
        }
        public static IServiceCollection RegisterHashAlgorithm(this IServiceCollection serviceCollection, TLSHashAlgorithm hashAlgorithm, Func <IDigest> factory)
        {
            return(serviceCollection.Update <HashAlgorithmRegistry>(prev =>
            {
                prev = prev ?? new HashAlgorithmRegistry();

                prev.Register(hashAlgorithm, factory);

                return prev;
            }));
        }
        public static SignedStream CreateSignedStream(this IServiceProvider serviceProvider, Stream stream, TLSHashAlgorithm hashAlgorithm, TLSSignatureAlgorithm signatureAlgorithm)
        {
            var signatureRegistry = serviceProvider.GetRequiredService <SignatureAlgorithmsRegistry>();
            var signatureCipherParameterFactoryProvider = serviceProvider.GetRequiredService <ISignatureCipherParameterFactoryProvider>();
            var hashRegistry = serviceProvider.GetRequiredService <HashAlgorithmRegistry>();

            var signature = signatureRegistry.Resolve(signatureAlgorithm);
            var signatureCipherFactory = signatureCipherParameterFactoryProvider.Create(signatureAlgorithm);
            var digest = hashRegistry.Resolve(hashAlgorithm);

            var endConfig = serviceProvider.GetRequiredService <EndConfig>();

            signature.Init(signatureCipherFactory.Create(endConfig.End, ConnectionDirection.Write));

            return(new SignedStream(stream, signature, digest));
        }