Ejemplo n.º 1
0
        public static byte[] HashData(TpmAlgId algId, byte[] dataToHash)
        {
            if (dataToHash == null)
            {
                dataToHash = new byte[0];
            }

#if TSS_USE_BCRYPT
            string algName = Native.BCryptHashAlgName(algId);
            if (string.IsNullOrEmpty(algName))
            {
                Globs.Throw <ArgumentException>("HashData(): Unsupported hash algorithm " + algId);
                return(null);
            }

            var alg    = new BCryptAlgorithm(algName);
            var digest = alg.HashData(dataToHash);
            alg.Close();
            return(digest);
#else
            HashAlgorithm hashAlg = null;
            switch (algId)
            {
            case TpmAlgId.Sha1:
                hashAlg = new SHA1Managed();
                break;

            case TpmAlgId.Sha256:
                hashAlg = new SHA256Managed();
                break;

            case TpmAlgId.Sha384:
                hashAlg = new SHA384Managed();
                break;

            case TpmAlgId.Sha512:
                hashAlg = new SHA512Managed();
                break;

            default:
                Globs.Throw <ArgumentException>("AlgId is not a supported hash algorithm");
                return(null);
            }
            return(hashAlg.ComputeHash(dataToHash));
#endif
        }
Ejemplo n.º 2
0
        public static byte[] HashData(TpmAlgId algId, byte[] dataToHash)
        {
#if TSS_USE_BCRYPT
            string algName = Native.BCryptHashAlgName(algId);
            if (string.IsNullOrEmpty(algName))
            {
                Globs.Throw<ArgumentException>("HashData(): Unsupported hash algorithm " + algId);
                return null;
            }

            var alg = new BCryptAlgorithm(algName);
            var digest = alg.HashData(dataToHash);
            alg.Close();
            return digest;
#else
            HashAlgorithm hashAlg = null;
            switch (algId)
            {
                case TpmAlgId.Sha1:
                    hashAlg = new SHA1Managed();
                    break;
                case TpmAlgId.Sha256:
                    hashAlg = new SHA256Managed();
                    break;
                case TpmAlgId.Sha384:
                    hashAlg = new SHA384Managed();
                    break;
                case TpmAlgId.Sha512:
                    hashAlg = new SHA512Managed();
                    break;
                default:
                    Globs.Throw<ArgumentException>("AlgId is not a supported hash algorithm");
                    return null;
            }
            return hashAlg.ComputeHash(dataToHash);
#endif
        }