本文整理汇总了C#中System.Security.Cryptography.SHA256Managed类的典型用法代码示例。如果您正苦于以下问题:C# SHA256Managed类的具体用法?C# SHA256Managed怎么用?C# SHA256Managed使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SHA256Managed类属于System.Security.Cryptography命名空间,在下文中一共展示了SHA256Managed类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HashValues
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;
}
示例2: GenerateSaltedHash
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();
}
示例3: SHA256
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);
}
示例4: GetBundleVirtualPath
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();
}
示例5: EncryptUserPassword
/// <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;
}
示例6: SHA256
/// <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字节的字符串
}
示例7: Sha256
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"));
}
示例8: GetHashSha256
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));
}
示例9: Encrypt
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();
}
示例10: Server
/// <summary>
/// konstruktor obiektu Server
/// </summary>
public Server()
{
requestReceived += Server_requestReceived;
aesKeyGenerator = new AesManaged();
hashAlgorithm = new SHA256Managed();
rsaKeyGenerator = new RSACryptoServiceProvider();
}
示例11: HexSha256
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));
}
示例12: checkPassword
//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;
}
}
示例13: SHA256
public static byte[] SHA256(byte[] data, int offset, int count)
{
using (var sha = new SHA256Managed())
{
return sha.ComputeHash(data, offset, count);
}
}
示例14: FromByteArray
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());
}
示例15: SHA256
/// <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×Ö½ÚµÄ×Ö·û´®
}