Example #1
0
 public string GetHashSha256(string token)
 {
     var bytes = Encoding.UTF8.GetBytes(token);
     var hasher = new SHA256Managed();
     var hash = hasher.ComputeHash(bytes);
     return hash.Aggregate(string.Empty, (current, x) => current + string.Format("{0:x2}", x));
 }
Example #2
0
 /// <summary>
 /// </summary>
 /// <param name="data"></param>
 /// <returns></returns>
 public static string Sha256(string data)
 {
     byte[] bytes = Encoding.UTF8.GetBytes(data);
     var hashstring = new SHA256Managed();
     byte[] hash = hashstring.ComputeHash(bytes);
     return hash.Aggregate(string.Empty, (current, x) => current + String.Format("{0:x2}", x));
 }
Example #3
0
        public static string HexSha256(string text)
        {
            const string salt = "jk#=¤)\"gld\"2347X#Z472\\!";
            var hash = new SHA256Managed().ComputeHash(Encoding.ASCII.GetBytes(text + salt));

            return hash.Aggregate("", (current, x) => current + string.Format("{0:x2}", x));
        }
        /// <summary>
        /// Encrypts the user's password
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public string EncryptUserPassword(string password)
        {
            //return FormsAuthentication.HashPasswordForStoringInConfigFile(password, "md5");

            string salt = GenerateSalt();

            //start hash creation:
            System.Security.Cryptography.SHA256 sha256 = new System.Security.Cryptography.SHA256Managed();

            // password string to bytes
            byte[] sha256Bytes = System.Text.Encoding.UTF8.GetBytes(password);

            //password bytes to hash
            byte[] cryString = sha256.ComputeHash(sha256Bytes);

            // start final encrypted password
            string sha256Str = string.Empty;

            // create final encrypted password: bytes to hex string
            for (int i = 0; i < cryString.Length; i++)
            {
                sha256Str += cryString[i].ToString("X");
            }

            // concatenate hashed password + salt
            // sha256Str = sha256Str + salt;

            return sha256Str;
        }
Example #5
0
        public ActionResult UserMGMT()
        {
            var users = (from u in dbContext.users
                         join o in dbContext.organizations on u.organization_id equals o.organization_id
                         join r in dbContext.roles on u.role_id equals r.role_id
                         where u.active == true
                         select new User
            {
                Email = u.email,
                OrganizationId = u.organization_id,
                Password = u.password,
                RoleId = u.role_id,
                UserId = u.user_id,
                RoleName = r.role_name,
                OrgName = o.orginization_name
            });

            byte[] data;
            foreach (var u in users)
            {
                data = System.Text.Encoding.ASCII.GetBytes(u.Password);
                data = new System.Security.Cryptography.SHA256Managed().ComputeHash(data);
                String hash = System.Text.Encoding.ASCII.GetString(data);
                u.Password = hash;
            }


            return(View(users));
        }
Example #6
0
File: Utils.cs Project: ekople/VR2
        public static string GenerateSaltedHash(string userName, string pssWd)
        {
            if (userName ==null || pssWd == null)
            {
                return null;
            }
            byte[] userNameBytes = GetBytes(userName);
            byte[] pssWdBytes = GetBytes(pssWd);
            HashAlgorithm algorithm = new SHA256Managed();

            byte[] plainTextWithSaltBytes =
              new byte[userNameBytes.Length + pssWdBytes.Length];

            for (int i = 0; i < userNameBytes.Length; i++)
            {
                plainTextWithSaltBytes[i] = userNameBytes[i];
            }
            for (int i = 0; i < pssWdBytes.Length; i++)
            {
                plainTextWithSaltBytes[userNameBytes.Length + i] = pssWdBytes[i];
            }

            byte[] hash = algorithm.ComputeHash(plainTextWithSaltBytes);
            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < hash.Length; i++)
            {
                sBuilder.Append(hash[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }
Example #7
0
        public static byte[] Encrypt(byte[] payload, CryptoContext cryptoContext)
        {
            var csEncrypt = cryptoContext.CryptoStreamOut;
            var output = cryptoContext.OutputStream;
            output.Position = 0;
            output.SetLength(0);

            using (MemoryStream hashStream = new MemoryStream())
            {
                // hash

                SHA256Managed crypt = new SHA256Managed();

                hashStream.Write(BitConverter.GetBytes(Interlocked.Increment(ref cryptoContext.SendCounter)), 0, 8);
                hashStream.Write(payload, 0, payload.Length);
                hashStream.Write(cryptoContext.Algorithm.Key, 0, cryptoContext.Algorithm.Key.Length);
                var hashBuffer = hashStream.ToArray();

                byte[] validationCheckSum = crypt.ComputeHash(hashBuffer, 0, hashBuffer.Length);

                byte[] content = payload.Concat(validationCheckSum.Take(8)).ToArray();

                csEncrypt.Write(content, 0, content.Length);
                csEncrypt.Flush();
            }

            return output.ToArray();
        }
Example #8
0
        public string sha256encrypt(string phrase)
        {
            SHA256Managed sha256 = new SHA256Managed();
            byte[] hashedData = sha256.ComputeHash(encoder.GetBytes(phrase));

            return byteArrayToString(hashedData);
        }
 public static string SHA256(string strToHash)
 {
     System.Security.Cryptography.SHA256Managed sha256Obj = new System.Security.Cryptography.SHA256Managed();
     byte[] bytesToHash = System.Text.Encoding.UTF8.GetBytes(strToHash);
     bytesToHash = sha256Obj.ComputeHash(bytesToHash);
     return(BitConverter.ToString(bytesToHash).Replace("-", "").ToUpper());
 }
Example #10
0
 public static string Encryption(string source, out string salt)
 {
     salt = Guid.NewGuid().ToString();
     byte[] passwordAndSaltBytes = Encoding.UTF8.GetBytes(source + salt);
     byte[] hasBytes             = new System.Security.Cryptography.SHA256Managed().ComputeHash(passwordAndSaltBytes);
     return(Convert.ToBase64String(hasBytes));
 }
 public static string sha256encrypt(string phrase)
 {
     UTF8Encoding encoder = new UTF8Encoding();
     SHA256Managed sha256hasher = new SHA256Managed();
     byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(phrase));
     return byteArrayToString(hashedDataBytes);
 }
Example #12
0
 public ActionResult DevEditPost(DevEditViewModel editVm)
 {
     var repo = new Repository();
     var original = GetDevEditViewModel(editVm.Id);
     original.Message = editVm.Message;
     var asc = new WebClient().DownloadData(repo.GetASCLink(editVm.Id));
     string extracted;
     if(!CryptoHelper.VerifySig(asc, editVm.Message, out extracted))
     {
         ModelState.AddModelError("Message", "Incorrectly signed");
     }
     else
     {
         byte[] hash = null;
         using(SHA256 sha = new SHA256Managed())
         {
             hash = sha.ComputeHash(Encoding.UTF8.GetBytes(extracted));
         }
         if(repo.SaveHash(hash))
         {
             repo.UpdateDevViewModel(original);
             return RedirectToAction("Dev", "Main", new
             {
                 devId = editVm.Id
             });
         }
         else
         {
             ModelState.AddModelError("Message", "You can't replay an old message");
         }
     }
     return View("DevEdit", original);
 }
Example #13
0
        public static string EncryptPassword(string password, byte[] salt)
        {
            // Convert the plain string pwd into bytes
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(password);
            // Append salt to pwd before hashing
            byte[] combinedBytes = new byte[plainTextBytes.Length + salt.Length];
            System.Buffer.BlockCopy(plainTextBytes, 0, combinedBytes, 0, plainTextBytes.Length);
            System.Buffer.BlockCopy(salt, 0, combinedBytes, plainTextBytes.Length, salt.Length);

            // Create hash for the pwd+salt
            System.Security.Cryptography.HashAlgorithm hashAlgo = new System.Security.Cryptography.SHA256Managed();
            byte[] hash = hashAlgo.ComputeHash(combinedBytes);

            var hashString = GetString(hash);
            var saltString = GetString(salt);

            var passwordHash = string.IsNullOrWhiteSpace(saltString) ? hashString : $"{hashString}.{saltString}";

            var hashBytes   = GetBytes(hashString).ToArray();
            var hashString2 = GetString(hashBytes);

            if (hashString != hashString2)
            {
                throw new SecurityException("Hashes are not equal. There is some bug.");
            }

            return(passwordHash);
        }
        protected virtual string GetBundleVirtualPath(string prefix, string postfix, string[] parts)
        {
            if (parts == null || parts.Length == 0)
                throw new ArgumentException("parts");

            //calculate hash
            var hash = "";
            using (SHA256 sha = new SHA256Managed())
            {
                // string concatenation
                var hashInput = "";
                foreach (var part in parts)
                {
                    hashInput += part;
                    hashInput += ",";
                }

                byte[] input = sha.ComputeHash(Encoding.Unicode.GetBytes(hashInput));
                hash = HttpServerUtility.UrlTokenEncode(input);
            }
            //ensure only valid chars
            hash = SeoExtensions.GetSeName(hash);

            var sb = new StringBuilder(prefix);
            sb.Append(hash);
            sb.Append(postfix);
            return sb.ToString();
        }
Example #15
0
        /// <summary>
        /// Encrypts the user's password
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public string EncryptUserPassword(string password)
        {
            //return FormsAuthentication.HashPasswordForStoringInConfigFile(password, "md5");

            string salt = GenerateSalt();

            //start hash creation:
            System.Security.Cryptography.SHA256 sha256 = new System.Security.Cryptography.SHA256Managed();

            // password string to bytes
            byte[] sha256Bytes = System.Text.Encoding.UTF8.GetBytes(password);

            //password bytes to hash
            byte[] cryString = sha256.ComputeHash(sha256Bytes);

            // start final encrypted password
            string sha256Str = string.Empty;

            // create final encrypted password: bytes to hex string
            for (int i = 0; i < cryString.Length; i++)
            {
                sha256Str += cryString[i].ToString("X");
            }

            // concatenate hashed password + salt
            // sha256Str = sha256Str + salt;

            return(sha256Str);
        }
Example #16
0
        private string HashValues(string Value1, string Value2, string HashingAlgorithm)
        {
            string sHashingAlgorithm = "";
            if (String.IsNullOrEmpty(HashingAlgorithm))
                sHashingAlgorithm = "SHA-1";
            else
                sHashingAlgorithm = HashingAlgorithm;

            byte[] arrByte;

            if (sHashingAlgorithm == "SHA-1")
            {
                SHA1Managed hash = new SHA1Managed();
                arrByte = hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(Value1 + Value2));
            }
            else
            {
                SHA256Managed hash = new SHA256Managed();
                arrByte = hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(Value1 + Value2));
            }

            string s = "";
            for (int i = 0; i < arrByte.Length; i++)
            {
                s += arrByte[i].ToString("x2");
            }
            return s;
        }
Example #17
0
 //public static string SecurePublishCode(string input, String salt)
 //{
 //    return GenerateSHA256Hash(input + salt);
 //}
 public static string GenerateSHA256Hash(string input)
 {
     byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input);
     SHA256Managed sha256hashstring = new SHA256Managed();
     byte[] hash = sha256hashstring.ComputeHash(bytes);
     return Encoding.Default.GetString(hash);
 }
Example #18
0
 private static string createHash(string password)
 {
     SHA256Managed hash = new SHA256Managed();
     byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
     byte[] hashBytes = hash.ComputeHash(passwordBytes);
     return Encoding.UTF8.GetString(hashBytes);
 }
Example #19
0
 public static String SHA256(String text)
 {
     UTF8Encoding encoder = new UTF8Encoding();
     SHA256Managed sha256hasher = new SHA256Managed();
     byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(text));
     return System.Convert.ToBase64String(hashedDataBytes);
 }
Example #20
0
        //pass in entered PW as parameter; hashes it and compared to stored PW
        public bool comparePW(string enteredPW)//returns true if match; false if not
        {
            bool ret;

            if (IsHashed == false)
            {
                hashPW();
            }

            using (var sha = new System.Security.Cryptography.SHA256Managed())
            {
                byte[] textData = System.Text.Encoding.UTF8.GetBytes(enteredPW);
                byte[] hash     = sha.ComputeHash(textData);
                enteredPW = BitConverter.ToString(hash).Replace("-", String.Empty);
            }

            if (enteredPW == PW)
            {
                ret = true;
            }
            else
            {
                ret = false;
            }


            return(ret);
        }
Example #21
0
 /// <summary>
 /// SHA256函数
 /// </summary>
 /// /// <param name="str">原始字符串</param>
 /// <returns>SHA256结果</returns>
 public static string SHA256(string str)
 {
     byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
     SHA256Managed Sha256 = new SHA256Managed();
     byte[] Result = Sha256.ComputeHash(SHA256Data);
     return Convert.ToBase64String(Result);  //返回长度为44字节的字符串
 }
Example #22
0
 protected string ComputeHashString(string rawString)
 {
     System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
     SHA256 hashM = new SHA256Managed();
     return encoding.GetString(hashM.ComputeHash(encoding.GetBytes(rawString))).
         Replace(',', '.').Replace('\r', '.').Replace('\n', '.');
 }
Example #23
0
 /// <summary>
 /// konstruktor obiektu Server 
 /// </summary>
 public Server()
 {
     requestReceived += Server_requestReceived;
     aesKeyGenerator = new AesManaged();
     hashAlgorithm = new SHA256Managed();
     rsaKeyGenerator = new RSACryptoServiceProvider();      
 }
        //this method checks that passwords match in a login attempt
        public bool checkPassword()
        {
            Credentials userCredentials = SecurityUserDAO.getUserCredentials(username);
            if (userCredentials != null)
            {
                byte[] dbPass = Convert.FromBase64String(userCredentials.getPassword());

                byte[] userPass = createByteArrayFromString(password);
                byte[] dbSalt = createByteArrayFromString(userCredentials.getSalt());

                byte[] userSaltedPass = userPass.Concat(dbSalt).ToArray();

                HashAlgorithm algorithm = new SHA256Managed();
                byte[] hasheduserPass = algorithm.ComputeHash(userSaltedPass);

                bool match = compareByteArrays(dbPass, hasheduserPass);
                if (match)
                {
                    userId = userCredentials.getUserId();
                }
                return match;
            }
            else
            {
                return false;
            }
        }
Example #25
0
 public static byte[] SHA256(byte[] data, int offset, int count)
 {
     using (var sha = new SHA256Managed())
     {
         return sha.ComputeHash(data, offset, count);
     }
 }
Example #26
0
 private string GetHash(string key)
 {
     using var sha = new System.Security.Cryptography.SHA256Managed();
     byte[] textData = System.Text.Encoding.UTF8.GetBytes(key);
     byte[] hash     = sha.ComputeHash(textData);
     return(BitConverter.ToString(hash).Replace("-", string.Empty));
 }
Example #27
0
 public static string Sha256(string password)
 {
     SHA256Managed crypt = new SHA256Managed();
     string hash = String.Empty;
     byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(password), 0, Encoding.UTF8.GetByteCount(password));
     return crypto.Aggregate(hash, (current, bit) => current + bit.ToString("x2"));
 }
 public static string sha256Hash(string strToHash)
 {
     SHA256Managed crypt = new SHA256Managed();
     string hash = String.Empty;
     byte[] crypto = crypt.ComputeHash(Encoding.ASCII.GetBytes(strToHash), 0, Encoding.ASCII.GetByteCount(strToHash));
     return crypto.Aggregate(hash, (current, theByte) => current + theByte.ToString("x2"));
 }
Example #29
0
        public override string Encrypt(string data)
        {
            SHA256Managed sha256hasher = new SHA256Managed();

            try
            {
                UTF8Encoding encoder = new UTF8Encoding();

                byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(string.Format(base.hashFormat, data)));

                return Convert.ToBase64String(hashedDataBytes);
            }
            catch (Exception e)
            {
                var exception = new CryptologyException("SHA256Encryptor.Encrypt()", "An error occurred while encrypting.", e);
                exception.Data.Add("hashFormat", hashFormat);
                exception.Data.Add("data", data);

                throw exception;
            }
            finally
            {
                sha256hasher.Dispose();
            }
        }
Example #30
0
 /// <summary>
 ///     SHA256º¯Êý
 /// </summary>
 /// ///
 /// <param name="str">ԭʼ×Ö·û´®</param>
 /// <returns>SHA256½á¹û</returns>
 public static string SHA256(string str)
 {
     var SHA256Data = Encoding.UTF8.GetBytes(str);
     var Sha256 = new SHA256Managed();
     var Result = Sha256.ComputeHash(SHA256Data);
     return Convert.ToBase64String(Result); //·µ»Ø³¤¶ÈΪ44×Ö½ÚµÄ×Ö·û´®
 }
        public static void ExecuteMongo()
        {
            const string connectionString = "mongodb://localhost";
            var client = new MongoClient(connectionString);
            var server = client.GetServer();
            var database = server.GetDatabase("Polyglot");
            var directory = database.GetCollection<RootDirectory>("rootdirectory");
            directory.Remove(new QueryDocument());

            byte[] inputBytes = System.Text.Encoding.Unicode.GetBytes("password");//will need to change to being the user input
            SHA256Managed hashstring = new SHA256Managed();
            byte[] dbHash = hashstring.ComputeHash(inputBytes);

            directory.Insert(new RootDirectory()
            {
                _id = ObjectId.GenerateNewId().ToString(),
                un = "harageth",
                pw = System.Text.Encoding.UTF8.GetString(dbHash),//need to encrypt this password
                folders = new List<Folder>() { new Folder() { folderName = "firstFolder", files = new List<string>() { "temp1.txt", "file1.txt" } }, new Folder() { folderName = "secondFolder", files = new List<string>() { "temp2.txt", "file2.txt" } }, new Folder() { folderName = "thirdFolder", files = new List<string>() { "temp3.txt", "file3.txt" } } },
                files = new List<string>( ) { "temp.txt", "file.txt" }

            });

            directory.Insert(new RootDirectory()
            {
                _id = ObjectId.GenerateNewId().ToString(),
                un = "Guest",
                //folders = new List<Folder>() { new Folder() { folderName = "firstFolder", files = new List<string>() { "temp1.txt", "file1.txt" } }, new Folder() { folderName = "secondFolder", files = new List<string>() { "temp2.txt", "file2.txt" } }, new Folder() { folderName = "thirdFolder", files = new List<string>() { "temp3.txt", "file3.txt" } } },
                //files = new List<string>() { "temp.txt", "file.txt" }

            });
        }
Example #32
0
        protected virtual string GetBundleVirtualPath(string prefix, string extension, string[] parts)
        {
            if (parts == null || parts.Length == 0)
                throw new ArgumentException("parts");

            //calculate hash
            var hash = "";
            using (SHA256 sha = new SHA256Managed())
            {
                // string concatenation
                var hashInput = "";
                foreach (var part in parts)
                {
                    hashInput += part;
                    hashInput += ",";
                }

                byte[] input = sha.ComputeHash(Encoding.Unicode.GetBytes(hashInput));
                hash = HttpServerUtility.UrlTokenEncode(input);
            }
            //ensure only valid chars
            hash = SeoExtensions.GetSeName(hash);

            var sb = new StringBuilder(prefix);
            sb.Append(hash);
            //we used "extension" when we had "runAllManagedModulesForAllRequests" set to "true" in web.config
            //now we disabled it. hence we should not use "extension"
            //sb.Append(extension);
            return sb.ToString();
        }
Example #33
0
 public static string SHA256(string str)
 {
     byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
     System.Security.Cryptography.SHA256Managed sHA256Managed = new System.Security.Cryptography.SHA256Managed();
     byte[] inArray = sHA256Managed.ComputeHash(bytes);
     return(System.Convert.ToBase64String(inArray));
 }
Example #34
0
 public static string Encryption(string source, out string salt)
 {
     salt = Guid.NewGuid().ToString();
     byte[] passwordAndSaltBytes = Encoding.UTF8.GetBytes(source + salt);
     byte[] hasBytes = new System.Security.Cryptography.SHA256Managed().ComputeHash(passwordAndSaltBytes);
     return Convert.ToBase64String(hasBytes);
 }
Example #35
0
        // Taken from http://dotnetpulse.blogspot.com/2007/12/sha1-hash-calculation-in-c.html
        private static string Sha1HashOfString(string input)
        {
            byte[] buffer = Encoding.Unicode.GetBytes(input);
            var crypto = new SHA256Managed();

            return BitConverter.ToString(crypto.ComputeHash(buffer)).Replace("-", "");
        }
Example #36
0
 public static string Sha256Hash(byte[] ba)
 { 
     SHA256Managed sha2 = new SHA256Managed();
     byte[] ba2 = sha2.ComputeHash(ba);
     sha2 = null;
     return BitConverter.ToString(ba2).Replace("-", string.Empty).ToLower();
 }
Example #37
0
        public void ButtonRegister_Click()
        {
            string username = "******";
            string password = "******";
            // random salt
            string salt = Guid.NewGuid().ToString();

            // random salt
            // you can also use RNGCryptoServiceProvider class
            //System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            //byte[] saltBytes = new byte[36];
            //rng.GetBytes(saltBytes);
            //string salt = Convert.ToBase64String(saltBytes);
            //string salt = ToHexString(saltBytes);

            byte[] passwordAndSaltBytes = System.Text.Encoding.UTF8.GetBytes(password + salt);
            byte[] hashBytes            = new System.Security.Cryptography.SHA256Managed().ComputeHash(passwordAndSaltBytes);

            string hashString = Convert.ToBase64String(hashBytes);

            Console.WriteLine(hashString);
            Console.WriteLine(salt);
            // you can also use ToHexString to convert byte[] to string
            //string hashString = ToHexString(hashBytes);

            //连接数据库
            //var db = new TestEntities();
            //usercredential newRecord = usercredential.Createusercredential(username, hashString, salt);
            //db.usercredentials.AddObject(newRecord);
            //db.SaveChanges();
        }
 public static String FromByteArray(Byte[] b, Byte version)
 {
     SHA256 sha256 = new SHA256Managed();
     b = (new Byte[] { version }).Concat(b).ToArray();
     Byte[] hash = sha256.ComputeHash(sha256.ComputeHash(b)).Take(4).ToArray();
     return Base58String.FromByteArray(b.Concat(hash).ToArray());
 }
Example #39
0
 public static byte[] SHA256(string str)
 {
     SHA256 sha = new SHA256Managed();
     sha.Initialize();
     //return sha.ComputeHash(str.ToByteArray());
     return sha.ComputeHash(Encoding.UTF8.GetBytes(str));
 }
Example #40
0
 public static byte[] SHA256(string str)
 {
     byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
     System.Security.Cryptography.SHA256Managed Sha256 = new System.Security.Cryptography.SHA256Managed();
     byte[] by = Sha256.ComputeHash(SHA256Data);
     return(by);
 }
Example #41
0
    static byte[] GenerateSaltedHash(String passwordString, byte[] salt)
    {
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(passwordString + salt);
        System.Security.Cryptography.SHA256Managed bytesToHash = new System.Security.Cryptography.SHA256Managed();
        byte[] hash = bytesToHash.ComputeHash(bytes);

        return(hash);
    }
Example #42
0
         /// <summary>
         /// SHA256加密,不可逆转
         /// </summary>
         /// <param name="str">string str:被加密的字符串</param>
         /// <returns>返回加密后的字符串</returns>
         private static string SHA256Encrypt(string str)
 {
     System.Security.Cryptography.SHA256 s256 = new System.Security.Cryptography.SHA256Managed();
     byte[] byte1;
     byte1 = s256.ComputeHash(Encoding.Default.GetBytes(str));
     s256.Clear();
     return(Convert.ToBase64String(byte1));
 }
        //public string GenerateSHA256Hash(string secretkey, string timestamp, string apiKey)
        //{
        //	try
        //	{
        //		var secretKeyByteArray = Convert.FromBase64String(secretkey);
        //		//var secretKeyByteArray = Encoding.UTF8.GetBytes(secretkey);
        //		string input = $"{apiKey}{timestamp}";
        //		byte[] signature = Encoding.UTF8.GetBytes(input);

        //		using (HMACSHA256 hmac = new HMACSHA256(secretKeyByteArray))
        //		{
        //			byte[] signatureBytes = hmac.ComputeHash(signature);

        //			return Convert.ToBase64String(signatureBytes);
        //		}
        //	}
        //	catch (Exception ex)
        //	{
        //		logger.Error(ex);
        //		return "";
        //	}
        //}

        public string GenerateSHA256Hash(string secretkey, string timestamp, string apikey, string recieptref)
        {
            string input = $"{secretkey}{timestamp}{apikey}{recieptref}";
            var    shama = new System.Security.Cryptography.SHA256Managed();

            byte[] crypto = shama.ComputeHash(Encoding.UTF8.GetBytes(input));
            return(Convert.ToBase64String(crypto));
        }
Example #44
0
        public static byte[] Sha256b(string input)
        {
            var crypt = new System.Security.Cryptography.SHA256Managed();
            var hash  = new System.Text.StringBuilder();

            byte[] crypto = crypt.ComputeHash(System.Text.Encoding.ASCII.GetBytes(input));
            return(crypto);
        }
Example #45
0
 /// <summary>
 /// sha256加密返回base64编码
 /// </summary>
 /// <param name="strIN"></param>
 /// <returns></returns>
 public static byte[] SHA256EncryptOutByte(string strIN)
 {
     System.Security.Cryptography.SHA256 s256 = new System.Security.Cryptography.SHA256Managed();
     byte[] byte1;
     byte1 = s256.ComputeHash(Encoding.Default.GetBytes(strIN));
     s256.Clear();
     return(byte1);
 }
Example #46
0
 public static byte[] Sha256(string hashInput)
 {
     System.Security.Cryptography.SHA256 sha256 = new System.Security.Cryptography.SHA256Managed();
     byte[] hash;
     hash = Encoding.ASCII.GetBytes(hashInput);
     hash = sha256.ComputeHash(hash);
     return(hash);
 }
Example #47
0
 //concatena un string y un salt y los hashea juntos.
 public static string HashIt(string toHash, string salt)
 {
     byte[] bytes = System.Text.Encoding.UTF8.GetBytes(toHash + salt);
     System.Security.Cryptography.SHA256Managed sha256String =
         new System.Security.Cryptography.SHA256Managed();
     byte[] newhash = sha256String.ComputeHash(bytes);
     return(Convert.ToBase64String(newhash));
 }
Example #48
0
        public string GenPassword(string password)
        {
            byte[] data = System.Text.Encoding.ASCII.GetBytes(password);
            data = new System.Security.Cryptography.SHA256Managed().ComputeHash(data);
            String hash = System.Text.Encoding.ASCII.GetString(data);

            return(hash);
        }
Example #49
0
        private static Byte[] obtenerEncriptacion(String value)
        {
            // ToDo: Mejorar esta seccion para una mejor encriptacion.

            using (var sha = new System.Security.Cryptography.SHA256Managed( )) {
                return(sha.ComputeHash(Encoding.UTF8.GetBytes(value)));
            }
        }
Example #50
0
    private String GenerateSHA256Hash(String input, String salt)
    {
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
        System.Security.Cryptography.SHA256Managed hashString = new System.Security.Cryptography.SHA256Managed();
        byte[] hash = hashString.ComputeHash(bytes);

        return(Convert.ToBase64String(hash));
    }
        public static string CreatePasswordHash(string inputString, string salt)
        {
            byte[] data = System.Text.Encoding.ASCII.GetBytes(salt + inputString);
            data = new System.Security.Cryptography.SHA256Managed().ComputeHash(data);
            String hash = System.Text.Encoding.ASCII.GetString(data);

            return(hash);
        }
Example #52
0
        /// <summary>
        /// SHA256加密
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        // ReSharper disable once InconsistentNaming
        public static string SHA256(string data)
        {
            var byteData = Encoding.UTF8.GetBytes(data);
            var sha256   = new SHA256Managed();
            var result   = sha256.ComputeHash(byteData);

            return(BitConverter.ToString(result).Replace("-", "").ToLower());
        }
Example #53
0
 public static string SHA256Encrypt(string str)
 {
     System.Security.Cryptography.SHA256 s256 = new System.Security.Cryptography.SHA256Managed();
     byte[] byte1;
     byte1 = s256.ComputeHash(Encoding.Default.GetBytes(str));
     s256.Clear();
     return(BitConverter.ToString(byte1).Replace("-", "").ToLower()); //64
 }
Example #54
0
 public static string SHA256Hash(string textinput, string salted)
 {
     byte[] bytes = System.Text.Encoding.UTF8.GetBytes(textinput + salted);
     System.Security.Cryptography.SHA256Managed sha256str =
         new System.Security.Cryptography.SHA256Managed();
     byte[] hash = sha256str.ComputeHash(bytes);
     return(ByteArrayToHexString(hash));
 }
Example #55
0
 public static String GenerateSHA256(String input, String salt)
 {
     byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
     System.Security.Cryptography.SHA256Managed sha256hashstring =
         new System.Security.Cryptography.SHA256Managed();
     byte[] hash = sha256hashstring.ComputeHash(bytes);
     return(BitConverter.ToString(hash));
 }
Example #56
0
 public string GenerateSHA256Hash(String input, String salt)
 {
     byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
     System.Security.Cryptography.SHA256Managed sHAHashString =
         new System.Security.Cryptography.SHA256Managed();
     byte[] hash = sHAHashString.ComputeHash(bytes);
     return(ByteArrayToHexString(hash));
 }
Example #57
0
 /// <summary>
 /// SHA256加密,不可逆转
 /// </summary>
 /// <param name="obj"></param>
 /// <returns></returns>
 public static string ToSHA256(this string obj)
 {
     System.Security.Cryptography.SHA256 s256 = new System.Security.Cryptography.SHA256Managed();
     byte[] byte1;
     byte1 = s256.ComputeHash(Encoding.Default.GetBytes(obj));
     s256.Clear();
     return(BitConverter.ToString(byte1).Replace("-", ""));
 }
Example #58
0
        private static string getHashString(string password, string salt)
        {
            byte[] passwordAndSaltBytes = System.Text.Encoding.UTF8.GetBytes(password + salt);
            byte[] hashBytes            = new System.Security.Cryptography.SHA256Managed().ComputeHash(passwordAndSaltBytes);
            string hashString           = Convert.ToBase64String(hashBytes);

            return(hashString);
        }
Example #59
0
        public String GjeneroSHA256Hashin(String hyrja, String salt)
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(hyrja + salt);
            System.Security.Cryptography.SHA256Managed sha256hashstring =
                new System.Security.Cryptography.SHA256Managed();
            byte[] hash = sha256hashstring.ComputeHash(bytes);

            return(Convert.ToBase64String(hash));
        }
Example #60
0
 public static string GetStringSha256Hash(string text)
 {
     using (var sha = new System.Security.Cryptography.SHA256Managed())
     {
         byte[] textData = System.Text.Encoding.UTF8.GetBytes(text);
         byte[] hash     = sha.ComputeHash(textData);
         return(BitConverter.ToString(hash).Replace("-", string.Empty));
     }
 }