Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Security > SHA1 encoding differences with FormsAuthentication and SHA1CryptoServiceProvider

Reply
Thread Tools

SHA1 encoding differences with FormsAuthentication and SHA1CryptoServiceProvider

 
 
Super Julius
Guest
Posts: n/a
 
      05-12-2004
Folks,

I am struggling with the following problem. When I encode a string
using FormsAuthentication or SHA1CryptoServiceProvider, I don't get
the same encoding.

In fact I have a SHA1 ASP implementation for one of our legacy
application but I have done the migration using the following code:

private string Hash(string toHash)
{
string hashed = "";

SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] hash = sha1.ComputeHash(System.Text.Encoding.UTF8.GetByte s(toHash));

foreach(byte b in hash)
hashed += Convert.ToString(b, 16).ToUpper();

return hashed;
}

I then noticed that some values were not encoded the same way. So I
tried using FormsAuthentication.HashPasswordForStoringInConfig File(value,
"SHA1"). Guess what the it encodes the values the same way the ASP
SHA1 does.

Basically this means that the code above with
SHA1CryptoServiceProvider is just wrong. I have tried using all the
encoding available when getting the bytes out of the string but I
cannot get the same encoding.

A value for which it does not work: ArntzHans

Result with SHA1CryptoServiceProvider:
1C4F53FA399F44D81BF4F8540B5127FB44EDA2

Result with FormsAuthentication:
1C4F53FA399F440D81BF4F8540B5127FB404EDA2
* *

Note that the 2 '0' characters outlined on the 2nd result are missing
from the first encoding.

I have read a few threads from users having the same problem, but no
concrete solution to the problem

Wish someone can help me solving this out

Thx
Julien
 
Reply With Quote
 
 
 
 
Hernan de Lahitte
Guest
Posts: n/a
 
      05-12-2004
Your problem is in the Hexa encoding loop. The ToString( b, 16) method gives
you a one char lenght for hexa values of one digit. I suggest you to use
this function for hexa encoding.

BitConverter.ToString( hash ).Replace( "-", string.Empty ).ToUpper()

--
Hernan de Lahitte
Lagash Systems S.A.
http://weblogs.asp.net/hernandl


This posting is provided "AS IS" with no warranties, and confers no rights.

"Super Julius" <> wrote in message
news: om...
> Folks,
>
> I am struggling with the following problem. When I encode a string
> using FormsAuthentication or SHA1CryptoServiceProvider, I don't get
> the same encoding.
>
> In fact I have a SHA1 ASP implementation for one of our legacy
> application but I have done the migration using the following code:
>
> private string Hash(string toHash)
> {
> string hashed = "";
>
> SHA1 sha1 = new SHA1CryptoServiceProvider();
> byte[] hash =

sha1.ComputeHash(System.Text.Encoding.UTF8.GetByte s(toHash));
>
> foreach(byte b in hash)
> hashed += Convert.ToString(b, 16).ToUpper();
>
> return hashed;
> }
>
> I then noticed that some values were not encoded the same way. So I
> tried using FormsAuthentication.HashPasswordForStoringInConfig File(value,
> "SHA1"). Guess what the it encodes the values the same way the ASP
> SHA1 does.
>
> Basically this means that the code above with
> SHA1CryptoServiceProvider is just wrong. I have tried using all the
> encoding available when getting the bytes out of the string but I
> cannot get the same encoding.
>
> A value for which it does not work: ArntzHans
>
> Result with SHA1CryptoServiceProvider:
> 1C4F53FA399F44D81BF4F8540B5127FB44EDA2
>
> Result with FormsAuthentication:
> 1C4F53FA399F440D81BF4F8540B5127FB404EDA2
> * *
>
> Note that the 2 '0' characters outlined on the 2nd result are missing
> from the first encoding.
>
> I have read a few threads from users having the same problem, but no
> concrete solution to the problem
>
> Wish someone can help me solving this out
>
> Thx
> Julien



 
Reply With Quote
 
 
 
 
Super Julius
Guest
Posts: n/a
 
      05-12-2004
Thanks Hernan for your answer.

You pointed right the issue. The problem was my convert to hex value
with Convert.ToString(b, 16).

I have not tested your solution as I fixed the issue just before your
post by using String.Format

Anyway I guess this can be relevant to other folks...

Here is the new code with

private string Hash(string toHash)
{
string hashed = "";

SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] hash =
sha1.ComputeHash(System.Text.Encoding.UTF8.GetByte s(toHash));

foreach(byte b in hash)
hashed += String.Format("{0,2:X2}", b);

return hashed;
}

Cheers
Julius

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Digitally Signing a XML Document (using SHA1+RSA or SHA1+DSA) Adam Tauno Williams Python 2 12-30-2010 10:23 AM
DEVELOP THE WINNING EDGE, SMALL DIFFERENCES IN YOUR PERFORMANCE CANLEAD TO LARGE DIFFERENCES IN YOUR RESULTS Home_Job_opportunity C Programming 0 01-14-2009 03:51 PM
DEVELOP THE WINNING EDGE, SMALL DIFFERENCES IN YOUR PERFORMANCE CANLEAD TO LARGE DIFFERENCES IN YOUR RESULTS Home_Job_opportunity C Programming 0 01-08-2009 04:31 PM
Form sha1.hexdigest to sha1.digest LMZ Python 5 04-06-2008 09:50 PM
SHA1 length of resulting hash Dil via .NET 247 ASP .Net 0 08-04-2004 12:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57