Example #1
0
        SHA256CryptoServiceProvider _hasher = new SHA256CryptoServiceProvider(); // using FIPS-compliant provider instead of MD5

        public string ComputeHash(params string[] sqls)
        {
            var sql  = string.Join(Environment.NewLine, sqls);
            var hash = HexUtil.ByteArrayToHex(_hasher.ComputeHash(Encoding.UTF8.GetBytes(sql)));

            return(hash);
        }
Example #2
0
        public static string BytesToLiteral(object value)
        {
            Util.CheckParam(value, nameof(value));
            var str = "0x" + HexUtil.ByteArrayToHex(DbDriverUtil.GetBytes(value));

            return(str);
        }
Example #3
0
 public static string BytesToLiteral(object value)
 {
     Util.CheckParam(value, nameof(value));
     byte[] bytes = DbDriverUtil.GetBytes(value);
     Util.Check(bytes != null, "Bytes to literal: invalid input value type {0}", value.GetType());
     return("x'" + HexUtil.ByteArrayToHex(bytes) + "'");
 }
Example #4
0
        private static string ComputeHash(string password, byte[] salt, int iterationCount, int hashSize)
        {
            var hasher  = new System.Security.Cryptography.Rfc2898DeriveBytes(password, salt, iterationCount);
            var bytes   = hasher.GetBytes(hashSize);
            var strHash = HexUtil.ByteArrayToHex(bytes);

            return(strHash);
        }
Example #5
0
        public static string Encrypt(string value, string purpose = "Generic")
        {
            var bytes     = Encoding.Unicode.GetBytes(value);
            var encrBytes = MachineKey.Protect(bytes, purpose);
            var result    = HexUtil.ByteArrayToHex(encrBytes);

            return(result);
        }
        public override Linq.Translation.SqlGen.SqlStatement GetLiteral(Guid literal)
        {
            var bytes  = literal.ToByteArray();
            var hex    = HexUtil.ByteArrayToHex(bytes);
            var result = "X'" + hex + "'";

            return(result);
        }
Example #7
0
        private void GenerateAndPrintKey(SymmetricAlgorithm alg)
        {
            alg.GenerateKey();
            alg.GenerateIV();
            var algName = alg.GetType().Name.PadRight(30);
            var hexKey  = HexUtil.ByteArrayToHex(alg.Key);

            Debug.WriteLine($"Algorithm: {algName}, keySize(bits): {alg.KeySize}, key: {hexKey}");
        }
Example #8
0
 public static string BytesToLiteral(object value)
 {
     if (value == null || value == DBNull.Value)
     {
         return(SqlTerms.Null.Text);
     }
     byte[] bytes = DbDriverUtil.GetBytes(value);
     Util.Check(bytes != null, "Bytes to literal: invalid input value type {0}", value.GetType());
     return("hextoraw('" + HexUtil.ByteArrayToHex(bytes) + "')");
 }
Example #9
0
        private static string BytesToLiteral(DbTypeInfo typeInfo, object value)
        {
            if (value == null || value == DBNull.Value)
            {
                return("NULL");
            }
            var bytes = (byte[])value;

            return(@"E'\\x" + HexUtil.ByteArrayToHex(bytes) + "'");
        }
Example #10
0
        private static string BytesToLiteral(object value)
        {
            if (value == null || value == DBNull.Value)
            {
                return("NULL");
            }
            var bytes = DbDriverUtil.GetBytes(value);

            return(@"E'\\x" + HexUtil.ByteArrayToHex(bytes) + "'");
        }
Example #11
0
        public string ComputeMd5(string value)
        {
            // FIPS-compliant MD5
            var md5     = SHA256CryptoServiceProvider.Create();
            var bytes   = Encoding.UTF8.GetBytes(value);
            var hash    = md5.ComputeHash(bytes);
            var hashStr = HexUtil.ByteArrayToHex(hash);

            return(hashStr);
        }
Example #12
0
        public static string ToLogString(this byte[] values, string prefix = null)
        {
            const int MaxTake = 64;

            if (values == null)
            {
                return("(null)");
            }
            var bytes = values.Length > MaxTake?values.Take(MaxTake).ToArray() : values;

            var hex    = HexUtil.ByteArrayToHex(bytes);
            var suffix = (values.Length > MaxTake) ? " ..." : string.Empty;

            return(Util.SafeFormat("{0}{1}{2}", prefix, hex, suffix));
        }
Example #13
0
        public static string BytesToLiteral(object value)
        {
            Util.CheckParam(value, nameof(value));
            switch (value)
            {
            case Guid g:
                return("0x" + HexUtil.ByteArrayToHex(g.ToByteArray()));

            case byte[] bytes:
                return("0x" + HexUtil.ByteArrayToHex(bytes));

            case Binary bin:
                return("0x" + HexUtil.ByteArrayToHex(bin.GetBytes()));

            default:
                Util.Throw("BytesToLiteral: invalid input value type {0}", value.GetType());
                return(null); //never happends
            }
        }
Example #14
0
 private void GenerateKey(SymmetricAlgorithm alg)
 {
     alg.GenerateKey();
     alg.GenerateIV();
     Debug.WriteLine("Algorithm: " + alg.GetType().Name.PadRight(30) + " keySize(bits): " + alg.KeySize + " key: " + HexUtil.ByteArrayToHex(alg.Key));
 }
Example #15
0
        public static string BinaryToLiteral(object value)
        {
            var bin = (Binary)value;

            return("0x" + HexUtil.ByteArrayToHex(bin.GetBytes()));
        }
        public static string BytesToLiteral(DbTypeInfo typeDef, object value)
        {
            var bytes = (byte[])value;

            return("0x" + HexUtil.ByteArrayToHex(bytes));
        }
Example #17
0
        private static string ConvertBinaryToLiteral(DbTypeInfo typeInfo, object value)
        {
            var bytes = (byte[])value;

            return("x'" + HexUtil.ByteArrayToHex(bytes) + "'");
        }