public void DoFailuresWithWmiEnabled() { int numberOfEvents = 50; using (WmiEventWatcher eventListener = new WmiEventWatcher(numberOfEvents)) { HashAlgorithmProvider hashProvider = new HashAlgorithmProvider(typeof(SHA1Managed), false); InstrumentationAttacherFactory attacherFactory = new InstrumentationAttacherFactory(); IInstrumentationAttacher binder = attacherFactory.CreateBinder(hashProvider.GetInstrumentationEventProvider(), new object[] { "foo", true, true, true }, new ConfigurationReflectionCache()); binder.BindInstrumentation(); for (int i = 0; i < numberOfEvents; i++) { try { hashProvider.CreateHash(null); } catch (Exception) { } } eventListener.WaitForEvents(); Assert.AreEqual(numberOfEvents, eventListener.EventsReceived.Count); } }
/// <summary> /// Hashes the specified value using SHA-1. /// </summary> /// <param name="value">The value to hash.</param> /// <returns> /// A base-64 encoded SHA-1 hash. /// </returns> public string HashSha1(string value) { var hashProvider = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1); var byteData = Encoding.UTF8.GetBytes(value); var buffer = CryptographicBuffer.CreateFromByteArray(byteData); var hash = hashProvider.HashData(buffer); return(CryptographicBuffer.EncodeToHexString(hash)); }
/** * 加密 password 函数 * @param {string} input 需加密信息 * @return {string} 加密后的 16 进制串 */ public static string EncodePsd(string input) { var alg = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5); buff = CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8); var hashed = alg.HashData(buff); return(CryptographicBuffer.EncodeToHexString(hashed)); }
public async Task BlockBlobWriteStreamBasicTestAsync() { byte[] buffer = GetRandomBuffer(3 * 1024 * 1024); CryptographicHash hasher = HashAlgorithmProvider.OpenAlgorithm("MD5").CreateHash(); CloudBlobClient blobClient = GenerateCloudBlobClient(); blobClient.ParallelOperationThreadCount = 2; string name = GetRandomContainerName(); CloudBlobContainer container = blobClient.GetContainerReference(name); try { await container.CreateAsync(); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); using (MemoryStream wholeBlob = new MemoryStream()) { BlobRequestOptions options = new BlobRequestOptions() { StoreBlobContentMD5 = true, }; using (IOutputStream writeStream = await blob.OpenWriteAsync(null, options, null)) { Stream blobStream = writeStream.AsStreamForWrite(); for (int i = 0; i < 3; i++) { await blobStream.WriteAsync(buffer, 0, buffer.Length); await wholeBlob.WriteAsync(buffer, 0, buffer.Length); Assert.AreEqual(wholeBlob.Position, blobStream.Position); hasher.Append(buffer.AsBuffer()); } await blobStream.FlushAsync(); } string md5 = CryptographicBuffer.EncodeToBase64String(hasher.GetValueAndReset()); await blob.FetchAttributesAsync(); Assert.AreEqual(md5, blob.Properties.ContentMD5); using (MemoryOutputStream downloadedBlob = new MemoryOutputStream()) { await blob.DownloadToStreamAsync(downloadedBlob); TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob.UnderlyingStream); } } } finally { container.DeleteAsync().AsTask().Wait(); } }
private string GetFileNameForKey(string key) { //generate md5 for key var alg = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5); var buff = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8); var hashed = alg.HashData(buff); return(CryptographicBuffer.EncodeToHexString(hashed) + ".jpg"); }
// From: // http://stackoverflow.com/questions/8299142/how-to-generate-md5-hash-code-for-my-winrt-app-using-c private static string ComputeMD5(string str) { var alg = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5); IBuffer buff = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8); var hashed = alg.HashData(buff); var res = CryptographicBuffer.EncodeToHexString(hashed); return(res); }
/// <summary> /// Hashes the specified data bytes. /// </summary> /// <param name="hashBytes">Data to hash.</param> /// <returns> /// Hashed bytes /// </returns> protected override byte[] Hash(byte[] hashBytes) { var sha1 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256); IBuffer buffer = sha1.HashData(CryptographicBuffer.CreateFromByteArray(hashBytes)); byte[] hash; CryptographicBuffer.CopyToByteArray(buffer, out hash); return(hash); }
/// <summary> /// Creates a signature value given a signature base and the consumer secret and a known token secret. /// </summary> /// <seealso cref="http://oauth.net/core/1.0#rfc.section.9.2"/> /// <param name="signatureMethod">The hashing method</param> /// <param name="signatureTreatment">The treatment to use on a signature value</param> /// <param name="signatureBase">The signature base</param> /// <param name="consumerSecret">The consumer secret</param> /// <param name="tokenSecret">The token secret</param> /// <returns></returns> public static string GetSignature(OAuthSignatureMethod signatureMethod, OAuthSignatureTreatment signatureTreatment, string signatureBase, string consumerSecret, string tokenSecret) { if (tokenSecret.IsNullOrBlank()) { tokenSecret = string.Empty; } consumerSecret = UrlEncodeRelaxed(consumerSecret); tokenSecret = UrlEncodeRelaxed(tokenSecret); string signature; switch (signatureMethod) { case OAuthSignatureMethod.HmacSha1: { #if !WINDOWS_UWP HMACSHA1 crypto = new HMACSHA1(); string key = "{0}&{1}".FormatWith(consumerSecret, tokenSecret); crypto.Key = encoding.GetBytes(key); signature = signatureBase.HashWith(crypto); #else signature = signatureBase.HashWith(HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1)); #endif break; } case OAuthSignatureMethod.HmacSha256: { HMACSHA256 crypto = new HMACSHA256(); string key = "{0}&{1}".FormatWith(consumerSecret, tokenSecret); crypto.Key = encoding.GetBytes(key); signature = signatureBase.HashWith(crypto); break; } case OAuthSignatureMethod.PlainText: { signature = "{0}&{1}".FormatWith(consumerSecret, tokenSecret); break; } default: throw new NotImplementedException("Only HMAC-SHA1 and HMAC-SHA256 are currently supported."); } string result = signatureTreatment == OAuthSignatureTreatment.Escaped ? UrlEncodeRelaxed(signature) : signature; return(result); }
/// <summary> /// Hash end point /// </summary> /// <param name="remoteEndPoint">Endpoint to hash</param> /// <returns></returns> public string Encode(EndPoint remoteEndPoint) { var buffer = CryptographicBuffer.ConvertStringToBinary(this.HashKey, BinaryStringEncoding.Utf8); var hashAlgorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256); var hashBuffer = hashAlgorithm.HashData(buffer); return(CryptographicBuffer.EncodeToBase64String(hashBuffer)); }
public static string CreateHash(string str) { var alg = HashAlgorithmProvider.OpenAlgorithm("MD5"); IBuffer buff = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8); var hashed = alg.HashData(buff); var res = CryptographicBuffer.EncodeToHexString(hashed); return(res); }
public static string HashWith(this string input, HashAlgorithmProvider algorithm) { throw new NotImplementedException("got to figure out how to do this"); // var data = Encoding.UTF8.GetBytes(input); // var hash = algorithm.HashData(CryptographicBuffer.ConvertStringToBinary(data)); // //var hash = algorithm.CreateHash()(data); // return Convert.ToBase64String(CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, hash)); }
public static string Encrypt(byte[] str) { var alg = HashAlgorithmProvider.OpenAlgorithm("MD5"); var buff = CryptographicBuffer.CreateFromByteArray(str); var hashed = alg.HashData(buff); var res = CryptographicBuffer.EncodeToHexString(hashed); return(res); }
public static byte[] ComputeSHA256(byte[] data) { var algorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256); var buffer = CryptographicBuffer.CreateFromByteArray(data); var hash = algorithm.HashData(buffer); CryptographicBuffer.CopyToByteArray(hash, out byte[] digest); return(digest); }
public static string MD5(string data) { var input = CryptographicBuffer.ConvertStringToBinary(data, BinaryStringEncoding.Utf8); var hasher = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5); var hashed = hasher.HashData(input); var digest = CryptographicBuffer.EncodeToBase64String(hashed); return(digest); }
public static string GetMd5RuntimeString(string source) { var hashAlgorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5); var buffer = CryptographicBuffer.ConvertStringToBinary(source, BinaryStringEncoding.Utf8); var digest = hashAlgorithm.HashData(buffer); var str = CryptographicBuffer.EncodeToHexString(digest).ToUpper(); return(str); }
public string CreateSha256Hash(string input) { IBuffer inputBuffer = CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8); var hasher = HashAlgorithmProvider.OpenAlgorithm("SHA256"); IBuffer hashed = hasher.HashData(inputBuffer); return(CryptographicBuffer.EncodeToBase64String(hashed)); }
internal static string CalculateMD5Hash(string input) { var alg = HashAlgorithmProvider.OpenAlgorithm("MD5"); IBuffer buff = CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8); var hashed = alg.HashData(buff); var res = CryptographicBuffer.EncodeToHexString(hashed); return(res.ToLower()); }
//hashing algorithm public static string GetHashString(string inputString) { var bufferString = CryptographicBuffer.ConvertStringToBinary(inputString, BinaryStringEncoding.Utf8); var hashAlgorithmProvider = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5); var bufferHash = hashAlgorithmProvider.HashData(bufferString); return(CryptographicBuffer.EncodeToHexString(bufferHash)); }
public static string Sha256Hash(string data) { IBuffer input = CryptographicBuffer.ConvertStringToBinary(data, BinaryStringEncoding.Utf8); IBuffer hashed = HashAlgorithmProvider.OpenAlgorithm("SHA256").HashData(input); return(CryptographicBuffer.EncodeToBase64String(hashed)); }
public string Hash(byte[] data) { var alg = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5); IBuffer buff = data.AsBuffer(); var hashed = alg.HashData(buff); var res = CryptographicBuffer.EncodeToHexString(hashed); return(res); }
private static string HashPassword(string password) { var passwordBuffer = CryptographicBuffer.ConvertStringToBinary(PASSWORD_HASH_SALT.Replace("your-password", password), BinaryStringEncoding.Utf8); var hashAlgorithmProvider = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1); var bufferHash = hashAlgorithmProvider.HashData(passwordBuffer); return(CryptographicBuffer.EncodeToHexString(bufferHash)); }
public string getHashSha256(string text, int length) { HashAlgorithmProvider hasher = HashAlgorithmProvider.OpenAlgorithm("SHA256"); IBuffer hashed = hasher.HashData(CryptographicBuffer.ConvertStringToBinary(text, BinaryStringEncoding.Utf8)); string textHex = CryptographicBuffer.EncodeToHexString(hashed); return(textHex.Substring(0, length)); }
private static string GetMD5HashBinHex(string val) { var hashAlgorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5); IBuffer buff = CryptographicBuffer.ConvertStringToBinary(val, BinaryStringEncoding.Utf8); var hashed = hashAlgorithm.HashData(buff); var res = CryptographicBuffer.EncodeToHexString(hashed); return(res); }
private object md5(string key) { var alg = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5); IBuffer buff = CryptographicBuffer.ConvertStringToBinary(secret_code, BinaryStringEncoding.Utf8); var hashed = alg.HashData(buff); var result = CryptographicBuffer.EncodeToHexString(hashed); return(result); }
private string MD5(string password) { string encrtpt_password; CryptographicHash objHash = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5).CreateHash(); objHash.Append(CryptographicBuffer.ConvertStringToBinary(password, BinaryStringEncoding.Utf16BE)); encrtpt_password = CryptographicBuffer.EncodeToBase64String(objHash.GetValueAndReset()); return(encrtpt_password); }
/// <summary> /// MD5 /// </summary> /// <param name="input"></param> /// <returns></returns> public static string ToMD5(string input) { var provider = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5); IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8); var hashed = provider.HashData(buffer); var result = CryptographicBuffer.EncodeToHexString(hashed); return(result); }
public static string ComputeSHA1(string str) { var encoded = Encoding.GetEncoding("ISO-8859-1").GetBytes(str); IBuffer buffMsg = CryptographicBuffer.CreateFromByteArray(encoded); HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1); IBuffer buffHash = objAlgProv.HashData(buffMsg); return(CryptographicBuffer.EncodeToHexString(buffHash)); }
//来源:https://blog.csdn.net/lindexi_gd/article/details/48951849 public static string GetMD5(string inputString) { CryptographicHash objHash = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5).CreateHash(); objHash.Append(CryptographicBuffer.ConvertStringToBinary(inputString, BinaryStringEncoding.Utf8)); IBuffer buffHash1 = objHash.GetValueAndReset(); return(CryptographicBuffer.EncodeToHexString(buffHash1)); }
public static string GetHash(string toHash) { var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5); var buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(toHash, BinaryStringEncoding.Utf8); var buffHashed = h.HashData(buffUtf8Msg); var hash = CryptographicBuffer.EncodeToBase64String(buffHashed); return(hash); }
public async Task <string> HashAsync(Stream stream) { var contentBuffer = await CreateBuffer(stream).ConfigureAwait(false); // Create first the buffer synchronously var hashAlgorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1); var hash = hashAlgorithm.HashData(contentBuffer); return(CryptographicBuffer.EncodeToBase64String(hash)); }
public static string Hashpass(string hashpass) { var a = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1); IBuffer buff = CryptographicBuffer.ConvertStringToBinary(hashpass, BinaryStringEncoding.Utf8); var hashed = a.HashData(buff); var res = CryptographicBuffer.EncodeToHexString(hashed); return(res); }
public void HashHMACSHA1FailsUsingHashAlgorithmProvider() { HashAlgorithmProviderData keyData = new HashAlgorithmProviderData(); keyData.AlgorithmType = typeof(HMACSHA1).AssemblyQualifiedName; keyData.SaltEnabled = false; keyData.Name = "BadHMACSHA1"; IHashProvider hashProvider = new HashAlgorithmProvider(); hashProvider.Initialize(new TestCryptographyConfigurationView(keyData)); hashProvider.CreateHash(plainText); }
private void Init(string algorithmName) { _hasher = HashAlgorithmProvider.OpenAlgorithm(algorithmName); Clear(); }
public void HashFailureThrowsWithInstrumentationEnabled() { HashAlgorithmProvider hashProvider = new HashAlgorithmProvider(typeof(SHA1Managed), false, new NullHashAlgorithmInstrumentationProvider()); hashProvider.CreateHash(null); }
public void HashWithBadType() { HashAlgorithmProviderData data = new HashAlgorithmProviderData(); data.AlgorithmType = "bad type"; data.Name = "bad"; IHashProvider hashProvider = new HashAlgorithmProvider(); hashProvider.Initialize(new TestCryptographyConfigurationView(data)); hashProvider.CreateHash(plainText); }
public void HashWithBadTypeThrows() { HashAlgorithmProvider hashProvider = new HashAlgorithmProvider(typeof(Exception), false, new NullHashAlgorithmInstrumentationProvider()); hashProvider.CreateHash(plainText); }