本文整理汇总了C#中System.Security.Cryptography.SHA1CryptoServiceProvider.ComputeHash方法的典型用法代码示例。如果您正苦于以下问题:C# SHA1CryptoServiceProvider.ComputeHash方法的具体用法?C# SHA1CryptoServiceProvider.ComputeHash怎么用?C# SHA1CryptoServiceProvider.ComputeHash使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.SHA1CryptoServiceProvider
的用法示例。
在下文中一共展示了SHA1CryptoServiceProvider.ComputeHash方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetGeneratedDatabasePincode
public string GetGeneratedDatabasePincode (string plainPincode)
{
SHA1CryptoServiceProvider sha1Provider = new SHA1CryptoServiceProvider ();
string pincode = string.Empty;
if(!string.IsNullOrEmpty (plainPincode)){
pincode = plainPincode;
}
string key = string.Empty;
#if DEBUG
key = "XI134=fV$/IP/ [email protected]|3a0iI:-R-|<*6C>`!F,Np9O;L1KhT";
#else
key += "35" +
Android.OS.Build.Board.Length % 10 + Android.OS.Build.Brand.Length % 10 +
Android.OS.Build.CpuAbi.Length % 10 + Android.OS.Build.Device.Length % 10 +
Android.OS.Build.Display.Length % 10 + Android.OS.Build.Host.Length % 10 +
Android.OS.Build.Id.Length % 10 + Android.OS.Build.Manufacturer.Length % 10 +
Android.OS.Build.Model.Length % 10 + Android.OS.Build.Product.Length % 10 +
Android.OS.Build.Tags.Length % 10 + Android.OS.Build.Type.Length % 10 +
Android.OS.Build.User.Length % 10;
#endif
int hashRounds = 0;
char[] charArray = key.ToCharArray ();
Array.Reverse (charArray);
hashRounds = Android.OS.Build.Board.Length % 2 == 0 ? 1 : 2;
hashRounds += Android.OS.Build.Brand.Length % 2 == 0 ? 1 : 2;
hashRounds += Android.OS.Build.Model.Length % 2 == 0 ? 1 : 2;
hashRounds += Android.OS.Build.Device.Length % 2 == 0 ? 1 : 2;
hashRounds += Android.OS.Build.Product.Length % 2 == 0 ? 1 : 2;
string salt = Convert.ToBase64String (sha1Provider.ComputeHash (Encoding.Default.GetBytes (new string (charArray))));
for (int i = 0; i < hashRounds; i++) {
if (string.IsNullOrEmpty (pincode)) {
pincode = key;
}
pincode += i % 2 == 0 ? salt : string.Empty;
byte[] originalBytes = Encoding.Default.GetBytes (pincode);
byte[] encodedBytes = sha1Provider.ComputeHash (originalBytes);
pincode = string.Empty;
foreach (byte bit in encodedBytes) {
pincode += bit.ToString ("x2");
}
}
SaveValueToKeyChain (StorageConstants.SaltStorageKey, salt);
return pincode;
}
示例2: Get411Password
/// <summary>
/// Returns a byte array containing the proper encryption of the
/// given password/seed according to the new 4.1.1 authentication scheme.
/// </summary>
/// <param name="password"></param>
/// <param name="seed"></param>
/// <returns></returns>
private byte[] Get411Password(string password, byte[] seedBytes)
{
// if we have no password, then we just return 1 zero byte
if (password.Length == 0) return new byte[1];
#if !NETSTANDARD1_6
SHA1 sha = new SHA1CryptoServiceProvider();
#else
SHA1 sha = SHA1.Create();
#endif
#if !NETSTANDARD1_6
byte[] firstHash = sha.ComputeHash(AliasText.Encoding.Default.GetBytes(password));
#else
byte[] firstHash = sha.ComputeHash(AliasText.Encoding.UTF8.GetBytes(password));
#endif
byte[] secondHash = sha.ComputeHash(firstHash);
byte[] input = new byte[seedBytes.Length + secondHash.Length];
Array.Copy(seedBytes, 0, input, 0, seedBytes.Length);
Array.Copy(secondHash, 0, input, seedBytes.Length, secondHash.Length);
byte[] thirdHash = sha.ComputeHash(input);
byte[] finalHash = new byte[thirdHash.Length + 1];
finalHash[0] = 0x14;
Array.Copy(thirdHash, 0, finalHash, 1, thirdHash.Length);
for (int i = 1; i < finalHash.Length; i++)
finalHash[i] = (byte)(finalHash[i] ^ firstHash[i - 1]);
return finalHash;
}
示例3: Get411Password
/// <summary>
/// Returns a byte array containing the proper encryption of the
/// given password/seed according to the new 4.1.1 authentication scheme.
/// </summary>
/// <param name="password"></param>
/// <param name="seed"></param>
/// <returns></returns>
public static byte[] Get411Password(string password, string seed)
{
// if we have no password, then we just return 2 zero bytes
if (password.Length == 0) return new byte[1];
#if NET451
SHA1 sha = new SHA1CryptoServiceProvider();
#else
SHA1 sha = SHA1.Create();
#endif
byte[] firstHash = sha.ComputeHash(Encoding.UTF8.GetBytes(password));
byte[] secondHash = sha.ComputeHash(firstHash);
byte[] seedBytes = Encoding.UTF8.GetBytes(seed);
byte[] input = new byte[seedBytes.Length + secondHash.Length];
Array.Copy(seedBytes, 0, input, 0, seedBytes.Length);
Array.Copy(secondHash, 0, input, seedBytes.Length, secondHash.Length);
byte[] thirdHash = sha.ComputeHash(input);
byte[] finalHash = new byte[thirdHash.Length + 1];
finalHash[0] = 0x14;
Array.Copy(thirdHash, 0, finalHash, 1, thirdHash.Length);
for (int i = 1; i < finalHash.Length; i++)
finalHash[i] = (byte)(finalHash[i] ^ firstHash[i - 1]);
return finalHash;
//byte[] buffer = new byte[finalHash.Length - 1];
//Array.Copy(finalHash, 1, buffer, 0, finalHash.Length - 1);
//return buffer;
}
示例4: HashPassword
/// <summary>
/// Creates a hash for the password.
/// </summary>
/// <param name="sessionId"> The current session id.</param>
/// <param name="password"> The password.</param>
/// <returns></returns>
private string HashPassword(string sessionId, string password)
{
SHA1CryptoServiceProvider hashProvider
= new SHA1CryptoServiceProvider();
string salt = Convert.ToBase64String(hashProvider.ComputeHash(System.Text.Encoding.UTF8.GetBytes(sessionId)));
byte[] hash = hashProvider.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password + salt));
return Convert.ToBase64String(hash);
}
开发者ID:molekilla,项目名称:Ecyware_GreenBlue_Inspector,代码行数:15,代码来源:LicenseServicesAuthenticationManager.cs
示例5: HashFilesEqual
private bool HashFilesEqual(byte[] contentFile1, byte[] contentFile2)
{
using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
{
string hashFile1 = BitConverter.ToString(sha1.ComputeHash(contentFile1)).Replace("-", string.Empty);
string hashFile2 = BitConverter.ToString(sha1.ComputeHash(contentFile2)).Replace("-", string.Empty);
return hashFile1 == hashFile2;
}
}
示例6: Initialize
private void Initialize()
{
rijndael = new RijndaelManaged() { Padding = PaddingMode.None };
aesInitializationVector = new byte[CRYPTO_BLOCK_SIZE];
int rawLength = 2 * password.Length;
byte[] rawPassword = new byte[rawLength + 8];
byte[] passwordBytes = Encoding.Unicode.GetBytes(password);
for (int i = 0; i < rawLength; i++)
{
rawPassword[i] = passwordBytes[i];
}
for (int i = 0; i < salt.Length; i++)
{
rawPassword[i + rawLength] = salt[i];
}
SHA1 sha = new SHA1CryptoServiceProvider();
const int noOfRounds = (1 << 18);
IList<byte> bytes = new List<byte>();
byte[] digest;
//TODO slow code below, find ways to optimize
for (int i = 0; i < noOfRounds; i++)
{
bytes.AddRange(rawPassword);
bytes.AddRange(new[] { (byte)i, (byte)(i >> 8), (byte)(i >> CRYPTO_BLOCK_SIZE) });
if (i % (noOfRounds / CRYPTO_BLOCK_SIZE) == 0)
{
digest = sha.ComputeHash(bytes.ToArray());
aesInitializationVector[i / (noOfRounds / CRYPTO_BLOCK_SIZE)] = digest[19];
}
}
digest = sha.ComputeHash(bytes.ToArray());
//slow code ends
byte[] aesKey = new byte[CRYPTO_BLOCK_SIZE];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
aesKey[i * 4 + j] = (byte)
(((digest[i * 4] * 0x1000000) & 0xff000000 |
(uint)((digest[i * 4 + 1] * 0x10000) & 0xff0000) |
(uint)((digest[i * 4 + 2] * 0x100) & 0xff00) |
(uint)(digest[i * 4 + 3] & 0xff)) >> (j * 8));
rijndael.IV = new byte[CRYPTO_BLOCK_SIZE];
rijndael.Key = aesKey;
rijndael.BlockSize = CRYPTO_BLOCK_SIZE * 8;
}
示例7: LoadDll
public static Assembly LoadDll(string dllname)
{
Assembly Target = Assembly.GetExecutingAssembly();
string resourceName = string.Format("{0}.Resources.{1}.dll", Target.GetName().Name, dllname);
byte[] assemblyData = null;
// Managed
using (Stream stream = Target.GetManifestResourceStream(resourceName))
{
assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
try
{
return Assembly.Load(assemblyData);
}
catch
{
// Purposely do nothing
// Unmanaged dll or assembly cannot be loaded directly from byte[]
// Let the process fall through for next part
}
}
// Unmanaged
bool fileOk = false;
string tempFile = Path.GetTempPath() + dllname + ".dll";
if (File.Exists(tempFile))
{
byte[] bb = File.ReadAllBytes(tempFile);
using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
{
string fileHash = BitConverter.ToString(sha1.ComputeHash(assemblyData)).Replace("-", string.Empty); ;
string fileHash2 = BitConverter.ToString(sha1.ComputeHash(bb)).Replace("-", string.Empty);
fileOk = (fileHash == fileHash2);
}
}
if (!fileOk)
{
File.WriteAllBytes(tempFile, assemblyData);
}
//return Assembly.LoadFile(tempFile);
LoadLibrary(tempFile);
return null;
}
示例8: EncodePassword
public static string EncodePassword(string password)
{
using (SHA1CryptoServiceProvider Sha = new SHA1CryptoServiceProvider())
{
return Convert.ToBase64String(Sha.ComputeHash(Encoding.ASCII.GetBytes(password)));
}
}
示例9: Sha1EncryptPassword
/// <summary>
/// Хэширует текст
/// </summary>
/// <param name="phrase"></param>
/// <returns></returns>
public static string Sha1EncryptPassword(string phrase)
{
var encoder = new UTF8Encoding();
var sha1Hasher = new SHA1CryptoServiceProvider();
var hashedDataBytes = sha1Hasher.ComputeHash(encoder.GetBytes(phrase));
return ByteArrayToString(hashedDataBytes);
}
示例10: ComputeHashFromStream
/// <summary>
/// Computes the hash for the specified Stream.
/// </summary>
/// <param name="stream">The stream for the input data.</param>
/// <returns>Hash value as a string.</returns>
public string ComputeHashFromStream(Stream stream)
{
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] hash = sha1.ComputeHash(stream);
return this.BytesToString(hash);
}
示例11: Main
static void Main(string[] args)
{
SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
Console.Write("Enter username: ");
string username = Console.ReadLine().ToUpper();
Console.Write("Enter password: ");
string password = Console.ReadLine().ToUpper();
string temp = username + ":" + password;
byte[] temp2 = Encoding.ASCII.GetBytes(temp);
sha.ComputeHash(temp2, 0, temp2.Length);
string hash = String.Empty;
for (int i = 0; i < sha.Hash.Length; i++)
{
hash += sha.Hash[i].ToString("X2");
}
Console.WriteLine("Hash: {0}", hash);
Console.ReadKey();
}
示例12: GenerateToken
public static string GenerateToken(string subdomainName, string ssoKey, Object userAttributes, int valid_for=5*60) {
string initVector = "OpenSSL for Ruby"; // DO NOT CHANGE
byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
byte[] keyBytesLong;
using( SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider() ) {
keyBytesLong = sha.ComputeHash( Encoding.UTF8.GetBytes( ssoKey + subdomainName ) );
}
byte[] keyBytes = new byte[16];
Array.Copy(keyBytesLong, keyBytes, 16);
var jsonText = JsonConvert.SerializeObject(userAttributes);
Dictionary<string, dynamic> dict = JsonConvert.DeserializeObject<Dictionary<string, dynamic> >(jsonText);
if (!dict.ContainsKey("expires")) {
dict["expires"] = DateTime.UtcNow.AddSeconds(valid_for).ToString("u");
//Console.WriteLine("Setting expires " + dict["expires"]);
}
byte[] textBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(dict));
for (int i = 0; i < 16; i++) {
textBytes[i] ^= initVectorBytes[i];
}
// Encrypt the string to an array of bytes
byte[] encrypted = EncryptStringToBytesWithAES(textBytes, keyBytes, initVectorBytes);
string encoded = Convert.ToBase64String(encrypted);
return HttpUtility.UrlEncode(encoded);
}
示例13: ComputeHash
/// <summary>
/// Computes the hash value for the specified string.
/// </summary>
/// <param name="text">The text for the input data.</param>
/// <returns>Hash value as a string.</returns>
public string ComputeHash(string text)
{
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] hash = sha1.ComputeHash(Encoding.Default.GetBytes(text));
return this.BytesToString(hash);
}
示例14: ComputeFileHash
internal static byte[] ComputeFileHash(byte[] fileBytes)
{
using (var sha1 = new SHA1CryptoServiceProvider())
{
return sha1.ComputeHash(fileBytes);
}
}
示例15: Sha1
public static string Sha1(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
SHA1CryptoServiceProvider cryptoTransformSHA1 = new SHA1CryptoServiceProvider();
byte[] outbuffer = cryptoTransformSHA1.ComputeHash(buffer);
return Convert.ToBase64String(outbuffer);
}