Пример #1
0
        public static byte[] Encrypt(string s)
        {
            var alg    = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
            var data   = Encoding.GetEncoding("iso-8859-1").GetBytes(s);
            var buff   = CryptographicBuffer.CreateFromByteArray(data);
            var hashed = alg.HashData(buff);
            var res    = CryptographicBuffer.EncodeToHexString(hashed);

            return(Encoding.GetEncoding("iso-8859-1").GetBytes(res));
        }
Пример #2
0
        public static byte[] HashCore(byte[] bytes)
        {
            var alg    = HashAlgorithmProvider.OpenAlgorithm("MD5");
            var buff   = CryptographicBuffer.CreateFromByteArray(bytes);
            var hashed = alg.HashData(buff);

            byte[] result;
            CryptographicBuffer.CopyToByteArray(hashed, out result);
            return(result);
        }
Пример #3
0
    /// <summary>
    /// Хеширование текста
    /// </summary>
    /// <param name="SourceText">Исходный текст для хэширования</param>
    /// <param name="HashAlgName">Название алгоритма хеширования</param>
    /// <returns>Хэш сообщения</returns>
    public string HashMode(string SourceText, string HashAlgName)
    {
        // Create a HashAlgorithmProvider object.
        HashAlgorithmProvider Hash = HashAlgorithmProvider.OpenAlgorithm(HashAlgName);
        IBuffer SourceTextBuffer   = CryptographicBuffer.ConvertStringToBinary(SourceText, BinaryStringEncoding.Utf16LE);
        // IBuffer SourceTextBuffer = CryptographicBuffer.DecodeFromBase64String(SourceText);
        IBuffer HashBuffer = Hash.HashData(SourceTextBuffer);

        return(CryptographicBuffer.EncodeToHexString(HashBuffer));
    }
Пример #4
0
        public static byte[] ComputeMD5(byte[] data)
        {
#if WINDOWS_PHONE
            var md5 = new MD5Managed();
            return(md5.ComputeHash(data));
#elif WIN_RT
            var md5 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
            return(md5.HashData(data.AsBuffer()).ToArray());
#endif
        }
Пример #5
0
        public static string CreateMD5(string param, string salt)
        {
            var     alg  = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
            IBuffer buff = CryptographicBuffer.ConvertStringToBinary(
                param + salt, BinaryStringEncoding.Utf8);
            var hashed = alg.HashData(buff);
            var res    = CryptographicBuffer.EncodeToHexString(hashed);

            return(res);
        }
Пример #6
0
        public static string GetDeviceId()
        {
            HardwareToken token      = HardwareIdentification.GetPackageSpecificToken(null);
            IBuffer       hardwareId = token.Id;

            HashAlgorithmProvider hasher = HashAlgorithmProvider.OpenAlgorithm("MD5");
            IBuffer hashed = hasher.HashData(hardwareId);

            return(CryptographicBuffer.EncodeToHexString(hashed));
        }
Пример #7
0
        public static byte[] sha1(byte[] input)
        {
            IBuffer buffer = CryptographicBuffer.CreateFromByteArray(input);
            HashAlgorithmProvider provider = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
            IBuffer hash = provider.HashData(buffer);

            byte[] ret;
            CryptographicBuffer.CopyToByteArray(hash, out ret);
            return(ret);
        }
Пример #8
0
        /// <summary>
        /// Krypterer streng med MD5-hashalgoritme
        /// </summary>
        /// <param name="streng">Tager streng som parameter</param>
        /// <returns>Returnerer krypteret streng</returns>
        public static string KrypterStreng(string streng)
        {
            const string salt     = "WmkqCmP4y4oi483xXOnb";
            var          alg      = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
            IBuffer      buff     = CryptographicBuffer.ConvertStringToBinary(streng + salt, BinaryStringEncoding.Utf8);
            var          hashed   = alg.HashData(buff);
            var          resultat = CryptographicBuffer.EncodeToHexString(hashed);

            return(resultat);
        }
Пример #9
0
        private static string CalculateSHA1(string text)
        {
            IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(text, BinaryStringEncoding.Utf8);
            HashAlgorithmProvider hashAlgorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);

            IBuffer hashBuffer = hashAlgorithm.HashData(buffer);
            var     hashHex    = CryptographicBuffer.EncodeToHexString(hashBuffer);

            return(hashHex);
        }
        public static string MD5Hash(string message)
        {
            IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(message, BinaryStringEncoding.Utf8);
            HashAlgorithmProvider hashAlgorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
            IBuffer hashBuffer = hashAlgorithm.HashData(buffer);

            var strHashBase64 = CryptographicBuffer.EncodeToBase64String(hashBuffer);

            return(strHashBase64);
        }
Пример #11
0
        /// <summary>
        /// 计算文件是否匹配
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string Md5(string str)
        {
            HashAlgorithmProvider hashAlgorithm =
                HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
            CryptographicHash cryptographic = hashAlgorithm.CreateHash();
            IBuffer           buffer        = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);

            cryptographic.Append(buffer);
            return(CryptographicBuffer.EncodeToHexString(cryptographic.GetValueAndReset()));
        }
Пример #12
0
        public static string ConvertStringToHash(string password, string salt)
        {
            byte[] data = Encoding.UTF8.GetBytes(password + "" + salt);
            HashAlgorithmProvider alg = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512);
            IBuffer buffer            = CryptographicBuffer.ConvertStringToBinary(password + salt, BinaryStringEncoding.Utf8);
            IBuffer hashed            = alg.HashData(buffer);
            string  res = CryptographicBuffer.EncodeToHexString(hashed);

            return(res);
        }
Пример #13
0
 public static string HashPassword(string toBeHashed)
 {
     {
         var     alg    = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
         IBuffer buff   = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary(toBeHashed, BinaryStringEncoding.Utf8);
         var     hashed = alg.HashData(buff);
         var     res    = CryptographicBuffer.EncodeToHexString(hashed);
         return(res);
     }
 }
Пример #14
0
        public static string ComputeSHA1Hash(this string input)
        {
            IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(input, BinaryStringEncoding.Utf8);
            HashAlgorithmProvider provider = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);

            //hash it
            IBuffer hash = provider.HashData(buffer);

            return(CryptographicBuffer.EncodeToHexString(hash).ToUpperInvariant());
        }
Пример #15
0
        /// <summary>
        /// Computes hash algorithm for the source string
        /// </summary>
        /// <param name="source">Source string to compute hash from</param>
        /// <param name="algorithm">HashAlgorithmNames.Sha1</param>
        /// <returns>hash from the source string</returns>
        public static string ComputeHash(string source, string algorithm)
        {
            HashAlgorithmProvider sha1 = HashAlgorithmProvider.OpenAlgorithm(algorithm);

            byte[]  bytes       = Encoding.UTF8.GetBytes(source);
            IBuffer bytesBuffer = CryptographicBuffer.CreateFromByteArray(bytes);
            IBuffer hashBuffer  = sha1.HashData(bytesBuffer);

            return(CryptographicBuffer.EncodeToHexString(hashBuffer));
        }
Пример #16
0
        private static string Md5Encrypt(string stringToEncrypt)
        {
            IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(stringToEncrypt, BinaryStringEncoding.Utf8);
            HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);

            IBuffer buffHash = objAlgProv.HashData(buffUtf8Msg);
            string  result   = CryptographicBuffer.EncodeToHexString(buffHash);

            return(result);
        }
Пример #17
0
        private string CreateCodeChallenge()
        {
            _codeVerifier = RandomNumberGenerator.CreateUniqueId();
            var sha256          = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
            var challengeBuffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(Encoding.UTF8.GetBytes(_codeVerifier)));

            byte[] challengeBytes;
            CryptographicBuffer.CopyToByteArray(challengeBuffer, out challengeBytes);
            return(Base64Url.Encode(challengeBytes));
        }
Пример #18
0
        /// <summary>
        /// 对指定 utf-8 字符串执行 MD5 校验
        /// </summary>
        /// <param name="str">注意编码格式为 utf-8</param>
        /// <returns></returns>
        public static string ComputeMD5(string str)
        {
            var alg = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);

            IBuffer buff   = CryptographicBuffer.CreateFromByteArray(Encoding.UTF8.GetBytes(str));
            var     hashed = alg.HashData(buff);
            var     res    = CryptographicBuffer.EncodeToHexString(hashed);

            return(res);
        }
Пример #19
0
        internal MD5Wrapper()
        {
#if WINDOWS_RT
            this.hash = HashAlgorithmProvider.OpenAlgorithm("MD5").CreateHash();
#elif WINDOWS_PHONE
            throw new NotSupportedException(SR.WindowsPhoneDoesNotSupportMD5);
#else
            this.hash = this.version1MD5 ? MD5.Create() : new NativeMD5();
#endif
        }
Пример #20
0
        public string GetDeviceID()
        {
            var     token       = HardwareIdentification.GetPackageSpecificToken(null);
            IBuffer buffer      = token.Id;
            var     md5Provider = HashAlgorithmProvider.OpenAlgorithm("MD5");
            var     hashdata    = md5Provider.HashData(buffer);


            return(CryptographicBuffer.EncodeToHexString(hashdata));
        }
Пример #21
0
        public static byte[] ComputeSHA1(byte[] data)
        {
#if WINDOWS_PHONE
            var sha1 = new SHA1Managed(); // to avoid thread sync problems http://stackoverflow.com/questions/12644257/sha1managed-computehash-occasionally-different-on-different-servers
            return(sha1.ComputeHash(data));
#elif WIN_RT
            var sha1 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
            return(sha1.HashData(data.AsBuffer()).ToArray());
#endif
        }
        private static string GenerateEncryptedPincode(string pincode)
        {
            HashAlgorithmProvider alg = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
            IBuffer buff   = CryptographicBuffer.ConvertStringToBinary(pincode, BinaryStringEncoding.Utf8);
            IBuffer hashed = alg.HashData(buff);
            string  res    = CryptographicBuffer.EncodeToHexString(hashed);

            LoggingService.Log("Pincode generated, now encrypting and returning it", LoggingLevel.Verbose);
            return(EncryptionService.Encrypt(res));
        }
Пример #23
0
        private string EncryptPassword(string password)
        {
            var input = CryptographicBuffer.ConvertStringToBinary(password,
                                                                  BinaryStringEncoding.Utf8);
            var hasher = HashAlgorithmProvider.OpenAlgorithm("SHA256");
            var hashed = hasher.HashData(input);
            var pwd    = CryptographicBuffer.EncodeToBase64String(hashed);

            return(pwd.ToString());
        }
Пример #24
0
        /// <summary>
        /// Asynchonrously computes the security token data represented by the password
        /// </summary>
        /// <returns>The SHA256 hash of the plaintext password</returns>
        public Task <IBuffer> GetBufferAsync()
        {
            var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
            var hash   = sha256.CreateHash();

            IBuffer passwordData = CryptographicBuffer.ConvertStringToBinary(this._password, BinaryStringEncoding.Utf8);

            hash.Append(passwordData);
            return(Task.FromResult(hash.GetValueAndReset()));
        }
Пример #25
0
        public static byte[] Sha1(string value)
        {
            HashAlgorithmProvider provider = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
            var data = CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8);
            var hash = provider.HashData(data);

            byte[] result;
            CryptographicBuffer.CopyToByteArray(hash, out result);
            return(result);
        }
Пример #26
0
        private async Task CloudFileUploadFromStreamAsync(CloudFileShare share, int size, long?copyLength, AccessCondition accessCondition, OperationContext operationContext, int startOffset)
        {
            byte[] buffer = GetRandomBuffer(size);
#if ASPNET_K
            MD5    hasher = MD5.Create();
            string md5    = Convert.ToBase64String(hasher.ComputeHash(buffer, startOffset, copyLength.HasValue ? (int)copyLength : buffer.Length - startOffset));
#else
            CryptographicHash hasher = HashAlgorithmProvider.OpenAlgorithm("MD5").CreateHash();
            hasher.Append(buffer.AsBuffer(startOffset, copyLength.HasValue ? (int)copyLength.Value : buffer.Length - startOffset));
            string md5 = CryptographicBuffer.EncodeToBase64String(hasher.GetValueAndReset());
#endif

            CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
            file.StreamWriteSizeInBytes = 512;

            using (MemoryStream originalFileStream = new MemoryStream())
            {
                originalFileStream.Write(buffer, startOffset, buffer.Length - startOffset);

                using (MemoryStream sourceStream = new MemoryStream(buffer))
                {
                    sourceStream.Seek(startOffset, SeekOrigin.Begin);
                    FileRequestOptions options = new FileRequestOptions()
                    {
                        StoreFileContentMD5 = true,
                    };
                    if (copyLength.HasValue)
                    {
                        await file.UploadFromStreamAsync(sourceStream, copyLength.Value, accessCondition, options, operationContext);
                    }
                    else
                    {
                        await file.UploadFromStreamAsync(sourceStream, accessCondition, options, operationContext);
                    }
                }

                await file.FetchAttributesAsync();

                Assert.AreEqual(md5, file.Properties.ContentMD5);

                using (MemoryStream downloadedFileStream = new MemoryStream())
                {
                    await file.DownloadToStreamAsync(downloadedFileStream);

                    Assert.AreEqual(copyLength ?? originalFileStream.Length, downloadedFileStream.Length);
                    TestHelper.AssertStreamsAreEqualAtIndex(
                        originalFileStream,
                        downloadedFileStream,
                        0,
                        0,
                        copyLength.HasValue ? (int)copyLength : (int)originalFileStream.Length);
                }
            }
        }
Пример #27
0
        public static byte[] ComputeMD5(string data)
        {
            var input  = CryptographicBuffer.ConvertStringToBinary(data, BinaryStringEncoding.Utf8);
            var hasher = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
            var hashed = hasher.HashData(input);

            byte[] digest;
            CryptographicBuffer.CopyToByteArray(hashed, out digest);

            return(digest);
        }
Пример #28
0
        static string ComputeHash(byte[] bytes)
        {
            HashAlgorithmProvider hasher = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512);

            IBuffer buffer   = bytes.AsBuffer();
            IBuffer buffHash = hasher.HashData(buffer);

            String strHashBase64 = CryptographicBuffer.EncodeToBase64String(buffHash);

            return(strHashBase64);
        }
Пример #29
0
        private string GenerateWebSocketAccept(HttpContext httpContext)
        {
            var webSocketKey      = httpContext.Request.Headers[HttpHeaderName.SecWebSocketKey];
            var responseKey       = webSocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
            var responseKeyBuffer = Encoding.ASCII.GetBytes(responseKey).AsBuffer();

            var sha1       = HashAlgorithmProvider.OpenAlgorithm("SHA1");
            var sha1Buffer = sha1.HashData(responseKeyBuffer);

            return(Convert.ToBase64String(sha1Buffer.ToArray()));
        }
Пример #30
0
        /// <summary>
        /// Получить уникальный ID.
        /// </summary>
        /// <param name="src">Данные.</param>
        /// <returns>ID.</returns>
        public static string CreateIdString(byte[] src)
        {
            var prov = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
            var hash = prov.CreateHash();

            hash.Append(CryptographicBuffer.CreateFromByteArray(src));
            var result = hash.GetValueAndReset();
            var bytes  = result.ToArray();

            return(ToHexString(bytes));
        }