本文整理汇总了C#中System.Security.Cryptography.SHA1Managed.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# SHA1Managed.Clear方法的具体用法?C# SHA1Managed.Clear怎么用?C# SHA1Managed.Clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.SHA1Managed
的用法示例。
在下文中一共展示了SHA1Managed.Clear方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Encrypt_static
/// <summary>
/// SHA1Managed 算法的哈希值大小为 160 位
/// 此类型的任何公共static成员都是线程安全的。但不保证所有实例成员都是线程安全的
/// </summary>
private static string Encrypt_static(string clearText, Encoding encode)
{
SHA1 sha = null;
try
{
byte[] originalBytes = encode.GetBytes(clearText); //Encoding.UTF8.GetBytes(clearText);
sha = new SHA1Managed();
byte[] data = sha.ComputeHash(originalBytes);
//return BitConverter.ToString(data); //将指定的字节数组的每个元素的数值转换为它的等效十六进制字符串表示形式
StringBuilder builder = new StringBuilder();
foreach (var item in data)
{
builder.Append(item.ToString("X2"));//将该哈希作为 32 字符的十六进制格式字符串返回
}
return builder.ToString();
}
catch (ArgumentNullException) { return clearText; }
catch (EncoderFallbackException) { return clearText; }
catch (ObjectDisposedException) { return clearText; }
catch (InvalidOperationException) { return clearText; }
catch (Exception) { return clearText; }
finally
{
if (sha != null)
{
sha.Clear();
sha.Dispose();
}
sha = null;
}
}
示例2: GetSHA1
public static string GetSHA1(string source)
{
var sha1 = new SHA1Managed();
var bytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(source));
var result = BitConverter.ToString(bytes).Replace("-", string.Empty).ToLower();
sha1.Clear();
return result;
}
示例3: SHA1Hash
/// <summary>
/// Encrypt a string depend on sha256 algorithm
/// </summary>
/// <param name="input"></param>
/// <returns>Encrypted string.</returns>
public static byte[] SHA1Hash(string input)
{
if (string.IsNullOrEmpty(input))
{
return null;
}
SHA1Managed hashAlgorithm = new SHA1Managed();
try
{
return hashAlgorithm.ComputeHash(Encoding.Default.GetBytes(input));
}
finally
{
hashAlgorithm.Clear();
}
}
示例4: GenerateSHA1
/// <summary>
/// Generates a base64 encoded SHA1 hash from the input string.
/// </summary>
/// <param name="input">The input string to be hashed.</param>
public static string GenerateSHA1(string input)
{
// extract password as unmanaged byte array
SHA1Managed sha = new SHA1Managed();
try
{
// generate hash using SHA1 provider
byte[] passwordData = Encoding.UTF8.GetBytes(input);
byte[] hashedPasswordData = sha.ComputeHash(passwordData);
string hashedPassword = Convert.ToBase64String(
hashedPasswordData);
// return hash
return hashedPassword;
}
// ensure the hash is cleared from memory
finally
{
sha.Clear();
}
}
示例5: SHAHash
static byte[] SHAHash(byte[] input)
{
SHA1Managed sha = new SHA1Managed();
byte[] output = sha.ComputeHash( input );
sha.Clear();
return output;
}
示例6: SHA1Encrypt
public string SHA1Encrypt(string strIN)
{
byte[] tmpByte;
SHA1 sha1 = new SHA1Managed();
tmpByte = sha1.ComputeHash(System.Text.Encoding.ASCII.GetBytes(strIN));
string strResult = BitConverter.ToString(tmpByte);
strResult = strResult.Replace("-", "");
sha1.Clear();
sha1 = null;
return strResult;
}
示例7: UpdateTmdContents
/// <summary>
/// Updates the Content Info in the Tmd.
/// Tmd and Contents must be in the same Directory
/// </summary>
/// <param name="tmdfile"></param>
public static void UpdateTmdContents(string tmdfile)
{
FileStream tmd = new FileStream(tmdfile, FileMode.Open, FileAccess.ReadWrite);
tmd.Seek(0x1de, SeekOrigin.Begin);
int contentcount = Tools.HexStringToInt(tmd.ReadByte().ToString("x2") + tmd.ReadByte().ToString("x2"));
for (int i = 0; i < contentcount; i++)
{
int oldsize = 0;
int contentpos = 0x1e4 + (36 * i);
tmd.Seek(contentpos, SeekOrigin.Begin);
string id = tmd.ReadByte().ToString("x2") + tmd.ReadByte().ToString("x2") + tmd.ReadByte().ToString("x2") + tmd.ReadByte().ToString("x2");
string index = "0000" + tmd.ReadByte().ToString("x2") + tmd.ReadByte().ToString("x2");
string type = tmd.ReadByte().ToString("x2") + tmd.ReadByte().ToString("x2");
if (type != "0001") continue;
try
{
oldsize = Tools.HexStringToInt(tmd.ReadByte().ToString("x2") +
tmd.ReadByte().ToString("x2") +
tmd.ReadByte().ToString("x2") +
tmd.ReadByte().ToString("x2") +
tmd.ReadByte().ToString("x2") +
tmd.ReadByte().ToString("x2") +
tmd.ReadByte().ToString("x2") +
tmd.ReadByte().ToString("x2"));
}
catch { }
byte[] oldsha1 = new byte[20];
tmd.Read(oldsha1, 0, oldsha1.Length);
string fileName = id;
if (!File.Exists(tmdfile.Remove(tmdfile.LastIndexOf('/') + 1) + fileName + ".app"))
fileName = index;
if (File.Exists(tmdfile.Remove(tmdfile.LastIndexOf('/') + 1) + fileName + ".app"))
{
byte[] content = Wii.Tools.LoadFileToByteArray(tmdfile.Remove(tmdfile.LastIndexOf('/') + 1) + fileName + ".app");
int newsize = content.Length;
if (newsize != oldsize)
{
byte[] changedsize = Tools.FileLengthToByteArray(newsize);
tmd.Seek(contentpos + 8, SeekOrigin.Begin);
for (int x = 8; x > changedsize.Length; x--) tmd.WriteByte(0x00);
tmd.Write(changedsize, 0, changedsize.Length);
}
SHA1Managed sha1 = new SHA1Managed();
byte[] newsha1 = sha1.ComputeHash(content);
sha1.Clear();
if (Tools.CompareByteArrays(newsha1, oldsha1) == false)
{
tmd.Seek(contentpos + 16, SeekOrigin.Begin);
tmd.Write(newsha1, 0, newsha1.Length);
}
}
else
{
throw new Exception("At least one content file wasn't found!");
}
}
tmd.Close();
}