예제 #1
0
    public void Test(DigestType type, string ikmHex, string saltHex, string infoHex, string prkHex, string okmHex)
    {
        Span <byte> prk = stackalloc byte[255];

        var length = Hkdf.Extract(type, ikmHex.FromHex(), saltHex.FromHex(), prk);

        Assert.AreEqual(prkHex, prk[..length].ToHex());
예제 #2
0
        public static HashAlgorithm GetHashAlgorithmByType(DigestType dgType)
        {
            HashAlgorithm ha = null;

            switch (dgType)
            {
            case DigestType.MD5:
                ha = MD5.Create();
                break;

            case DigestType.SHA1:
                ha = SHA1.Create();
                break;

            case DigestType.SHA256:
                ha = SHA256.Create();
                break;

            case DigestType.SHA384:
                ha = SHA384.Create();
                break;

            case DigestType.SHA512:
                ha = SHA512.Create();
                break;

            case DigestType.RIPEMD160:
                ha = RIPEMD160.Create();
                break;
            }
            return(ha);
        }
예제 #3
0
 public static HashAlgorithm GetHashAlgorithmByType(DigestType dgType)
 {
     HashAlgorithm ha = null;
     switch (dgType)
     {
         case DigestType.MD5:
             ha = MD5.Create();
             break;
         case DigestType.SHA1:
             ha = SHA1.Create();
             break;
         case DigestType.SHA256:
             ha = SHA256.Create();
             break;
         case DigestType.SHA384:
             ha = SHA384.Create();
             break;
         case DigestType.SHA512:
             ha = SHA512.Create();
             break;
         case DigestType.RIPEMD160:
             ha = RIPEMD160.Create();
             break;
     }
     return ha;
 }
예제 #4
0
        /// <summary>
        /// 计算字符串哈希
        /// </summary>
        /// <param name="strSrc">欲计算哈希的字符串</param>
        /// <param name="dgType">指定哈希算法</param>
        /// <returns>成功返回计算得到的哈希字节数组,否则为null</returns>
        public static byte[] StringDigest(string strSrc, DigestType dgType)
        {
            byte[]        bytsrc = Encoding.Default.GetBytes(strSrc);
            HashAlgorithm ha     = GetHashAlgorithmByType(dgType);

            return(ha.ComputeHash(bytsrc));
        }
예제 #5
0
        public unsafe static FrequencyResultSet DigestInput(string input, DigestType digestType)
        {
            FrequencyResultSet result = new FrequencyResultSet();

            int strLength = input.Length;

            fixed(char *pString = input)
            {
                char *pChar = pString;

                for (int i = 0; i < strLength; i++)
                {
                    var c = *pChar;

                    if (digestType.HasFlag(DigestType.Symbols))
                    {
                        UpdateSymbolFrequency(result.FoundSymbolFrequencyResultSet, c);
                    }

                    pChar++;

                    if (digestType.HasFlag(DigestType.Words))
                    {
                        UpdateWordFrequency(result.FoundWordFrequencyResultSet, c);
                    }
                }
            }

            return(result);
        }
예제 #6
0
 /// <summary>
 /// Two DnsResourceDataDelegationSigner are equal iff their key tag, algorithm, digest type and digest fields are equal.
 /// </summary>
 public bool Equals(DnsResourceDataDelegationSigner other)
 {
     return(other != null &&
            KeyTag.Equals(other.KeyTag) &&
            Algorithm.Equals(other.Algorithm) &&
            DigestType.Equals(other.DigestType) &&
            Digest.Equals(other.Digest));
 }
예제 #7
0
 /// <summary>
 ///   Gets the hash algorithm for the <see cref="DigestType"/>.
 /// </summary>
 /// <param name="digestType">
 ///   One of the <see cref="DigestType"/> values.
 /// </param>
 /// <returns>
 ///   A new instance of the <see cref="HashAlgorithm"/> that implements
 ///   the <paramref name="digestType"/>.
 /// </returns>
 /// <exception cref="NotImplementedException">
 ///   When <paramref name="digestType"/> is not implemented.
 /// </exception>
 public static HashAlgorithm Create(DigestType digestType)
 {
     if (Digests.TryGetValue(digestType, out Func <HashAlgorithm> maker))
     {
         return(maker());
     }
     throw new NotImplementedException($"Digest type '{digestType}' is not implemented.");
 }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="username"></param>
        /// <param name="privateKeyPath"></param>
        /// <param name="password"></param>
        /// <param name="keyType"></param>
        /// <param name="digestType"></param>
        public SSHPrivateKeyAuthenticator(String username, String privateKeyPath, String password, KeyType keyType = KeyType.PKCS1, DigestType digestType = DigestType.SSH_RSA_SHA256)
        {
            Username       = username;
            SignDigestType = digestType;

            if (privateKeyPath.Length > 0)
            {
                SetPrivateKey(privateKeyPath, password, keyType);
            }
        }
예제 #9
0
        /// <summary>
        /// 获取文件哈希值
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="dgType">指定哈希算法</param>
        /// <returns>成功返回计算得到的哈希字节数组,否则为null</returns>
        public static byte[] FileDigest(string filePath, DigestType dgType)
        {
            if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath)) return null;

            Stream sr = File.OpenRead(filePath);
            HashAlgorithm ha = GetHashAlgorithmByType(dgType);
            byte[] hashbyt = ha.ComputeHash(sr);
            sr.Close();
            return hashbyt;
        }
예제 #10
0
 public static IMac Create(DigestType type, ReadOnlySpan <byte> key)
 {
     return(type switch
     {
         DigestType.Md5 => Create(key, HashAlgorithmName.MD5),
         DigestType.Sha1 => Create(key, HashAlgorithmName.SHA1),
         DigestType.Sha256 => Create(key, HashAlgorithmName.SHA256),
         DigestType.Sha384 => Create(key, HashAlgorithmName.SHA384),
         DigestType.Sha512 => Create(key, HashAlgorithmName.SHA512),
         _ => Create(key, DigestUtils.Create(type))
     });
예제 #11
0
 public static byte[] FileDigest(string filePath, DigestType dgType)
 {
     if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
     {
         return null;
     }
     Stream inputStream = File.OpenRead(filePath);
     byte[] buffer = GetHashAlgorithmByType(dgType).ComputeHash(inputStream);
     inputStream.Close();
     return buffer;
 }
예제 #12
0
    public static int Extract(DigestType type, ReadOnlySpan <byte> ikm, ReadOnlySpan <byte> salt, Span <byte> prk)
    {
        var hashLength = HashLength(type);

        if (prk.Length < hashLength)
        {
            throw new ArgumentException(@"prk too small", nameof(prk));
        }

        if (prk.Length > hashLength)
        {
            prk = prk[..hashLength];
예제 #13
0
        /// <summary>
        /// 获取文件哈希值
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="dgType">指定哈希算法</param>
        /// <returns>成功返回计算得到的哈希字节数组,否则为null</returns>
        public static byte[] FileDigest(string filePath, DigestType dgType)
        {
            if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
            {
                return(null);
            }

            Stream        sr = File.OpenRead(filePath);
            HashAlgorithm ha = GetHashAlgorithmByType(dgType);

            byte[] hashbyt = ha.ComputeHash(sr);
            sr.Close();
            return(hashbyt);
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="username">Your store username</param>
        /// <param name="publicKeyFilePath">The path to your public key file associated with your store user</param>
        /// <param name="agentSocketPath">Optional path to the SSH Agent socket. Defaults to environment variable value SSH_AUTH_SOCK </param>
        /// <param name="digestType">A DigestType constant</param>
        public SSHAgentAuthenticator(String username, String publicKeyFilePath, String agentSocketPath = "", DigestType digestType = DigestType.SSH_RSA_SHA256)
        {
            Username       = username;
            SignDigestType = digestType;
            AgentClient    = new SSHAgentSignClient(agentSocketPath);

            if (publicKeyFilePath.Length > 0)
            {
                if (File.Exists(publicKeyFilePath))
                {
                    SetPublicKeyFromFile(publicKeyFilePath);
                }
            }
        }
예제 #15
0
 public static IHash Create(DigestType type)
 {
     return(type switch
     {
         DigestType.Sm3 => new SM3Digest(),
         DigestType.Md5 => new DefaultMD5Digest(),
         DigestType.Sha1 => new DefaultSHA1Digest(),
         DigestType.Sha256 => new DefaultSHA256Digest(),
         DigestType.Sha384 => new DefaultSHA384Digest(),
         DigestType.Sha512 => new DefaultSHA512Digest(),
         DigestType.Crc32 => CreateCrc32(),
         DigestType.Crc32C => CreateCrc32C(),
         _ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
     });
예제 #16
0
        public static NSec3HashAlgorithm ToNSec3HashAlgorithm(this DigestType digestType)
        {
            switch (digestType)
            {
            case DigestType.Sha1:
                return(NSec3HashAlgorithm.Sha1);

            case DigestType.Sha256:
            case DigestType.GostR34_11_94:
            case DigestType.Sha384:
            case DigestType.Sha512:
            default:
                throw new ArgumentOutOfRangeException(nameof(digestType), digestType, null);
            }
        }
예제 #17
0
        public static DnsSecDigestType ToDnsSecDigestType(this DigestType digestType)
        {
            switch (digestType)
            {
            case DigestType.Sha1:
                return(DnsSecDigestType.Sha1);

            case DigestType.Sha256:
                return(DnsSecDigestType.Sha256);

            case DigestType.GostR34_11_94:
                return(DnsSecDigestType.EccGost);

            case DigestType.Sha384:
                return(DnsSecDigestType.Sha384);

            case DigestType.Sha512:
            default:
                throw new ArgumentOutOfRangeException(nameof(digestType), digestType, null);
            }
        }
예제 #18
0
        public static HashAlgorithm GetHashAlgorithmByType(DigestType dgType)
        {
            switch (dgType)
            {
                case DigestType.MD5:
                    return MD5.Create();

                case DigestType.SHA1:
                    return SHA1.Create();

                case DigestType.SHA256:
                    return SHA256.Create();

                case DigestType.SHA384:
                    return SHA384.Create();

                case DigestType.SHA512:
                    return SHA512.Create();

                case DigestType.RIPEMD160:
                    return RIPEMD160.Create();
            }
            return null;
        }
예제 #19
0
        public void Bind(String dn, String password, DigestType digestType)
        {
            if (fCurrentConnection == null)
            {
                Open();
            }

            if (String.IsNullOrEmpty(dn) || String.IsNullOrEmpty(password))
            {
                digestType = DigestType.None;
            }

            switch (digestType)
            {
            case DigestType.MD5:
            {
                Int32 lSequenceId = SendLdapRequest(Asn1.LDAPBINDREQ,
                                                    new BerInteger(LdapVersion),
                                                    new BerString(Asn1.OCTETSTRING, ""),
                                                    new BerSequence(Asn1.LDAPBINDSASL, new BerString(Asn1.OCTETSTRING, "DIGEST-MD5")));

                Response lResponse = ReadResponse();
                if (lResponse.SequenceId != lSequenceId)
                {
                    throw new LdapException("Invalid sequence id in bind response");
                }
                if (lResponse.Code != 14 || lResponse.RestData == null || lResponse.RestData[0].Type != BerType.String)                                 // 14 = Sasl bind in progress
                {
                    throw new LdapException(lResponse.Code);
                }


                String lResult        = ((BerString)lResponse.RestData[0]).Value;
                String lEncodedResult = Sasl.MD5Login(lResult, dn, password, GetTargetHostName());

                lSequenceId = SendLdapRequest(Asn1.LDAPBINDREQ,
                                              new BerInteger(LdapVersion),
                                              new BerString(Asn1.OCTETSTRING, ""),
                                              new BerSequence(Asn1.LDAPBINDSASL, new BerString(Asn1.OCTETSTRING, lEncodedResult)));

                lResponse = ReadResponse();
                if (lResponse.SequenceId != lSequenceId)
                {
                    throw new LdapException("Invalid sequence id in bind response");
                }

                if (lResponse.Code != 14 || lResponse.RestData == null || lResponse.RestData[0].Type != BerType.String)                                 // 14 = Sasl bind in progress
                {
                    throw new LdapException(lResponse.Code);
                }

                lSequenceId = SendLdapRequest(Asn1.LDAPBINDREQ,
                                              new BerInteger(LdapVersion),
                                              new BerString(Asn1.OCTETSTRING, ""),
                                              new BerSequence(Asn1.LDAPBINDSASL, new BerString(Asn1.OCTETSTRING, "DIGEST-MD5")));

                lResponse = ReadResponse();
                if (lResponse.SequenceId != lSequenceId)
                {
                    throw new LdapException("Invalid sequence id in bind response");
                }

                if (lResponse.Code != 0)
                {
                    throw new LdapException(lResponse.Code);
                }
                break;
            }

            default:
            {
                Int32 lSequenceId = SendLdapRequest(Asn1.LDAPBINDREQ,
                                                    new BerInteger(LdapVersion),
                                                    new BerString(Asn1.OCTETSTRING, dn),
                                                    new BerString(Asn1.LDAPBINDPASSWORD, password));
                Response lResponse = ReadResponse();

                if (lResponse.SequenceId != lSequenceId)
                {
                    throw new LdapException("Invalid sequence id in bind response");
                }

                if (lResponse.Code != 0)
                {
                    throw new LdapException(lResponse.Code);
                }

                break;
            }
            }

            fLoggedIn = true;
        }
 public TokenAuthenticator SetDigestType(DigestType NewSigningDigestType)
 {
     SignDigestType = NewSigningDigestType;
     return(this);
 }
예제 #21
0
 /// <summary>
 /// 计算字符串哈希
 /// </summary>
 /// <param name="strSrc">欲计算哈希的字符串</param>
 /// <param name="dgType">指定哈希算法</param>
 /// <returns>成功返回计算得到的哈希字节数组,否则为null</returns>
 public static byte[] StringDigest(string strSrc, DigestType dgType)
 {
     byte[] bytsrc = Encoding.Default.GetBytes(strSrc);
     HashAlgorithm ha = GetHashAlgorithmByType(dgType);
     return ha.ComputeHash(bytsrc);
 }
 /// <summary>
 /// Constructor with pre loaded private key
 /// </summary>
 /// <param name="username"></param>
 /// <param name="privateKey"></param>
 /// <param name="digestType"></param>
 public SSHPrivateKeyAuthenticator(String username, RSA privateKey, DigestType digestType = DigestType.SSH_RSA_SHA256)
 {
     Username       = username;
     PrivateKey     = privateKey;
     SignDigestType = digestType;
 }
 /// <summary>
 /// Constructor with pre loaded x509
 /// </summary>
 /// <param name="username"></param>
 /// <param name="X509"></param>
 /// <param name="digestType"></param>
 public SSHPrivateKeyAuthenticator(String username, X509Certificate2 X509, DigestType digestType = DigestType.SSH_RSA_SHA256)
 {
     Username       = username;
     PrivateKey     = X509.GetRSAPrivateKey();
     SignDigestType = digestType;
 }
 /// <summary>
 /// Set the digest type
 /// </summary>
 /// <param name="newDigestType"></param>
 /// <returns></returns>
 public SSHAgentAuthenticator SetDigestType(DigestType newDigestType)
 {
     SignDigestType = newDigestType;
     return(this);
 }
 /// <summary>
 /// Set the digest type
 /// </summary>
 /// <param name="newDigestType"></param>
 /// <returns></returns>
 public SSHPrivateKeyAuthenticator SetDigestType(DigestType newDigestType)
 {
     SignDigestType = newDigestType;
     return(this);
 }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="apiToken">Your API Token</param>
 /// <param name="signingKey">Your signing key in base64 format</param>
 /// <param name="digestType">One of DigestType constants</param>
 public TokenAuthenticator(String apiToken, String signingKey, DigestType digestType)
 {
     ApiToken       = apiToken;
     SignDigestType = digestType;
     SetSigningKey(signingKey);
 }