473,395 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to create Hash SHA512 of a string in c #

i need a sample of Hash SHA512 with string value in c sharp for c sharp website
Oct 17 '15 #1
2 2617
adriancs
122 100+
Expand|Select|Wrap|Line Numbers
  1. public static string Sha512Hash(string input)
  2. {
  3.     byte[] ba = Encoding.UTF8.GetBytes(input);
  4.     SHA512Managed sha5 = new SHA512Managed();
  5.     byte[] ba2 = sha5.ComputeHash(ba);
  6.     sha5 = null;
  7.     return BitConverter.ToString(ba2).Replace("-", string.E
mpty).ToLower();
}
Dec 23 '15 #2
PsychoCoder
465 Expert Mod 256MB
Here is a method I use regularly to create a SHA512 of a string

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. public static string SHA512(string plainText, byte[] saltBytes)
  4.         {
  5.             if (saltBytes == null)
  6.             {
  7.                 int minSalt = 4;
  8.                 int maxSalt = 8;
  9.  
  10.                 rnd rnd = new rnd();
  11.                 int saltSize = rnd.Next(minSalt, maxSalt);
  12.                 saltBytes = new byte[saltSize];
  13.                 RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
  14.                 rng.GetNonZeroBytes(saltBytes);
  15.             }
  16.  
  17.             byte[] bytes = Encoding.UTF8.GetBytes(plainText);
  18.             byte[] textWithSaltBytes = new byte[bytes.Length + saltBytes.Length];
  19.  
  20.             for (int i = 0; i < bytes.Length; i++)
  21.             {
  22.                 textWithSaltBytes[i] = bytes[i];
  23.             }
  24.  
  25.             for (int i = 0; i < saltBytes.Length; i++)
  26.             {
  27.                 textWithSaltBytes[bytes.Length + i] = saltBytes[i];
  28.             }
  29.  
  30.             hshAlgorithm hsh;
  31.             hsh = new SHA512Managed();
  32.  
  33.             byte[] hshBytes = hsh.Computehsh(textWithSaltBytes);
  34.             byte[] hshWithSaltBytes = new byte[hshBytes.Length + saltBytes.Length];
  35.  
  36.             for (int i = 0; i < hshBytes.Length; i++)
  37.             {
  38.                 hshWithSaltBytes[i] = hshBytes[i];
  39.             }
  40.  
  41.             for (int i = 0; i < saltBytes.Length; i++)
  42.             {
  43.                 hshWithSaltBytes[hshBytes.Length + i] = saltBytes[i];
  44.             }
  45.  
  46.             string hshValue = Convert.ToBase64String(hshWithSaltBytes);
  47.  
  48.             return hshValue;
  49.         }
  50.  
Mar 1 '16 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Roomates Computer | last post by:
I have the following code, and I use this method to create a 1 MB file by running it 1024 times. After it writes the file, I tried to use ZIP to compress it, the file size is down to 50%!! How...
3
by: Miguel Dias Moura | last post by:
Hello, I am working on ASP.NET/VB. I have this string: Dim keywords() As String = Request.QueryString("search").Split(CChar("")) Now I want to define a new string like this: keywordsSQL =...
4
by: jesper_lofgren | last post by:
Hi there, I have some webcontrols that i want to add dynamically on a page. I have stored the path / namespace in database (ex MyNameSpace.WebControls.Control1) to the class/webcontrol. Lets...
3
by: devrobcom | last post by:
Anyone knowing a better way of creating a format string than this? WCHAR sw_arg; int val1, val2; WCHAR mstring; WCHAR inbuffer1 = L"-value1 100 -value2 1000 -string1 mystring\t"; int read_no2;...
8
by: =?gb2312?B?zrrW0Luq?= | last post by:
I want to create a multiline string that can works in ie and firefox, who have a better idea ?
2
by: db2db2db2 | last post by:
hello all, we know that db2 create index using B-Tree,but if i want to create index using hash structure,how can i do it? in my opinion, db2 usually create index using B-tree,my question is if db2...
1
by: ganesh22 | last post by:
hi.... Iam Passing User Id one page to anothere page by using query string in hashformat .now in that second page i want to retrive the original user Id not in hash format so what can i do??
5
by: NJonge01 | last post by:
Hi, I haven't even tried to code this yet, but I need to create a "search" string that would work like this: Read this: strText = Create this string StrCriteria = Like "*Word1*" and Like...
5
by: ericdaniel | last post by:
Hi, I'm new to Python and I need to do the following: from this: s = "978654321" to this : Any help is appreciated Thanks,
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.