Laedit
.Security.Cryptography.Hash
News
About
Softwares
Framework
Utils
Contact-us
Laedit
CodeDocumentation
Collections
Generic
MultiMap
Configuration
DynamicSettings
Convert
Extensions
ArrayExtensions
BitmapExtensions
BitmapSourceExtensions
EnumExtensions
EventHandlerExtensions
ListExtensions
ListViewExtensions
ObjectExtensions
SecureStringExtensions
StringExtensions
WindowExtensions
Games
Dice
DicesRoller
IO
Drive
Log
LogFile
USBManager
FilesProvider
Net
FTPClient
Web
Security
ConfigurationFileProtection
Cryptography
AxCryptHelper
Hash
Windows
WPF
Controls
WaitBox
PathNavigator
Converters
FileNameToExtensionConverter
XNameToStringConverter
Skin
DictionaryResourceGenerator
SkinManager
Skins
AquaGel
Glossy
Monochrome
Xml
Linq
IXElementable
Description
Code
using System; using System.Security.Cryptography; using System.Text; namespace Laedit.Security.Cryptography { /// <summary> /// Provides methods to get or check the hash of a string /// </summary> public class Hash { /// <summary> /// Hasj type /// </summary> public enum HashType { /// <summary> /// md5 /// </summary> MD5, /// <summary> /// sha1 /// </summary> SHA1, /// <summary> /// sha256 /// </summary> SHA256, /// <summary> /// sha512 /// </summary> SHA512 } /// <summary> /// Gets the hash. /// </summary> /// <param name="text">The text.</param> /// <param name="hashType">Type of the hash.</param> /// <returns>The hash from the string</returns> public static string GetHash(string text, HashType hashType) { HashAlgorithm _algorithm; switch (hashType) { case HashType.MD5: _algorithm = MD5.Create(); break; case HashType.SHA1: _algorithm = SHA1.Create(); break; case HashType.SHA256: _algorithm = SHA256.Create(); break; case HashType.SHA512: _algorithm = SHA512.Create(); break; default: throw new ArgumentException("Invalid hash type", "hashType"); } byte[] bytes = Encoding.Unicode.GetBytes(text); byte[] hash = _algorithm.ComputeHash(bytes); return Laedit.Convert.BytesToHexadecimalString(hash); } /// <summary> /// Checks the hash. /// </summary> /// <param name="original">The original.</param> /// <param name="hashString">The hash string.</param> /// <param name="hashType">Type of the hash.</param> /// <returns>True if the hash is valid; Otherwise false.</returns> public static bool CheckHash(string original, string hashString, HashType hashType) { string originalHash = GetHash(original, hashType); return (originalHash == hashString); } } }