473,804 Members | 2,296 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SHA1Managed class has different results in 2.0 vs. 1.1??

Bob
We currently have an application running on .NET 1.1. It hashes certain
data using System.Security .Cryptography.S HA1Managed class. It has worked
out fine until we upgraded the app to .NET 2.0. SHA1Managed in 2.0 hashes
to a different stirng output when the input is exactly the same. Why would
this be the case? I thought the SHA1 algorithm is the same regardless of
the actual implementation. Here's my source code, which compiles file in
both 1.1 and 2.0

public static string HashThis(string salt, string password) {
System.Text.ASC IIEncoding encoding=new
System.Text.ASC IIEncoding();
string saltedPassword = salt + password;
byte [] saltByte = encoding.GetByt es(saltedPasswo rd);
SHA1CryptoServi ceProvider sha = new
System.Security .Cryptography.S HA1CryptoServic eProvider();
sha.ComputeHash (saltByte);
return encoding.GetStr ing(sha.Hash);
}
Thanks a lot for any help.
Bob
Jan 18 '06 #1
2 2626
Bob
ALl right, figured out the problem right after I sent the question. It's an
ASCII encoding issue. ASCII encoding behaves differently in 2.0 and 1.1,
not the hashing itself.
"Bob" <bo*******@yaho o.com> wrote in message
news:Od******** *****@tk2msftng p13.phx.gbl...
We currently have an application running on .NET 1.1. It hashes certain
data using System.Security .Cryptography.S HA1Managed class. It has worked
out fine until we upgraded the app to .NET 2.0. SHA1Managed in 2.0 hashes
to a different stirng output when the input is exactly the same. Why
would this be the case? I thought the SHA1 algorithm is the same
regardless of the actual implementation. Here's my source code, which
compiles file in both 1.1 and 2.0

public static string HashThis(string salt, string password) {
System.Text.ASC IIEncoding encoding=new
System.Text.ASC IIEncoding();
string saltedPassword = salt + password;
byte [] saltByte = encoding.GetByt es(saltedPasswo rd);
SHA1CryptoServi ceProvider sha = new
System.Security .Cryptography.S HA1CryptoServic eProvider();
sha.ComputeHash (saltByte);
return encoding.GetStr ing(sha.Hash);
}
Thanks a lot for any help.
Bob

Jan 18 '06 #2
Bob <bo*******@yaho o.com> wrote:
We currently have an application running on .NET 1.1. It hashes certain
data using System.Security .Cryptography.S HA1Managed class. It has worked
out fine until we upgraded the app to .NET 2.0. SHA1Managed in 2.0 hashes
to a different stirng output when the input is exactly the same. Why would
this be the case? I thought the SHA1 algorithm is the same regardless of
the actual implementation. Here's my source code, which compiles file in
both 1.1 and 2.0

public static string HashThis(string salt, string password) {
System.Text.ASC IIEncoding encoding=new
System.Text.ASC IIEncoding();
string saltedPassword = salt + password;
byte [] saltByte = encoding.GetByt es(saltedPasswo rd);
SHA1CryptoServi ceProvider sha = new
System.Security .Cryptography.S HA1CryptoServic eProvider();
sha.ComputeHash (saltByte);
return encoding.GetStr ing(sha.Hash);
}


The problem is that your code is broken - it's converting from
arbitrary binary data to a string using an ASCII encoding. What do you
expect it to do when it comes across a byte outside the ASCII range
(i.e. anything over 127)?

Here's a program which demonstrates the problem:

using System;
using System.Text;

class Test
{
static void Main()
{
byte[] data = new byte[]{140};
string text = Encoding.ASCII. GetString(data) ;
Console.WriteLi ne ((int)text[0]);
}
}

Basically, you were relying on unspecified behaviour, and it's changed.
Now as to what you can do about that - the easiest thing would probably
be to emulate the previous behaviour. The simplest way of doing that is
something like:

static string OldBytesToAscii (byte[] data)
{
char[] c = new char[data.Length];
for (int i=0; i < data.Length; i++)
{
c[i] = (char)(data[i]&0x7f);
}
return new string (c);
}

A better solution for moving forward in the future is to base64 binary
data when you need it in a reliable text form.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 18 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
2243
by: Ron_Adam | last post by:
Hi, Thanks again for all the helping me understand the details of decorators. I put together a class to create decorators that could make them a lot easier to use. It still has a few glitches in it that needs to be addressed. (1) The test for the 'function' object needs to not test for a string but an object type instead.
166
8698
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
10
2984
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract class. The interface has implied abstract methods/properties and derived classes can inherit multiple interfaces. The interface properties/methods have no implementation. Besides definitions of the two, what are some conceptual reasons to use...
6
1258
by: Peter Hayes | last post by:
I've tried this on another group without any resolution, so let me see if anyone here has an answer - please! ;-) I have a database with which I must communicate queries via a web-based interface. The queries are not in a standard format, but involve strings such as ....&fieldA=value1&fieldA=value2&...fieldA=valuej&fieldB=valueK... being sent to the web interface.
16
2929
by: digitalorganics | last post by:
What's the difference between initializing class variables within the class definition directly versus initializing them within the class's __init__ method? Is there a reason, perhaps in certain situations, to choose one over the other? Thank you.
32
5833
by: Matias Jansson | last post by:
I come from a background of Java and C# where it is common practise to have one class per file in the file/project structure. As I have understood it, it is more common practice to have many classes in a Python module/file. What is the motivation behind it, would it be a bad idea to have a guideline in your project that promotes a one class per file structure (assuming most of the programmers a background similar to mine)?
4
2489
by: Joseph Geretz | last post by:
We use a Soap Header to pass a token class (m_Token) back and forth with authenticated session information. Given the following implementation for our Logout method, I vastly prefer to simply code m_Token = null in order to destroy the session token when the user logs out. However, I'm finding that setting class instance to null results in no header being sent back to the client, with the result that the client actually remains with an...
6
1397
by: tkpmep | last post by:
I have written a program that runs portfolio simulations with different parameters and prints the output, but am mystified by the behavior of a mutable class variable. A simplified version of the program follows - would you kindly help me understand why it behaves the way it does. The function main() reads some data and then repeatedly calls simulation() with different parameter values. Each time the simulation runs, it creates a...
1
1596
by: sunshine19992 | last post by:
Not sure if others have come acrossed this bit I have a program for a C# class I am taking and during my troubleshooting I have found that if I turn on a breakpoint and then press F5 to continue until the program finishes I am receiving different results than if I didn't insert a breakpoint. (Put breakpoint at 1st curly bracket after AddToSumCount(rollSum); line in RollDice() method): Here is a copy of my code if you want to see it: using...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10347
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9173
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.