CryptoUtils

This commit is contained in:
2015-06-04 00:19:01 +02:00
parent aead372b6c
commit 5933bd8afc
2 changed files with 39 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
namespace Scrummer.Code
{
public class CryptoUtils
{
public static string GetSHA1(string str)
{
SHA1 sha1 = SHA1Managed.Create();
UTF8Encoding encoding = new UTF8Encoding();
byte[] stream = null;
StringBuilder sb = new StringBuilder();
stream = sha1.ComputeHash(encoding.GetBytes(str));
for (int i = 0; i < stream.Length; i++) sb.AppendFormat("{0:x2}", stream[i]);
return sb.ToString();
}
public static string GetRandString(int len)
{
byte[] bytes = new byte[len];
var cryptoRandom = new System.Security.Cryptography.RNGCryptoServiceProvider();
cryptoRandom.GetBytes(bytes);
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetString(bytes);
}
public static string GetCryptoToken()
{
return CryptoUtils.GetSHA1(CryptoUtils.GetRandString(10));
}
}
}

View File

@@ -72,6 +72,7 @@
<Compile Include="Code\Controls\CLabel.cs" />
<Compile Include="Code\Controls\CTextBox.cs" />
<Compile Include="Code\Controls\IValidableControl.cs" />
<Compile Include="Code\CryptoUtils.cs" />
<Compile Include="Code\Entities\Message.cs" />
<Compile Include="Code\GlobalErrorHandler.cs" />
<Compile Include="Code\JSON\ParserContext.cs" />