473,396 Members | 1,892 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,396 software developers and data experts.

String conversion

Hi

I'm maintaining a VisualC++ project to increase its security regarding
stored passwords.

I thought about using SHA256Managed to create a hash for the password
when creating a user and when this new user tries to login, a new hash
will be created for the given password and compared to the stored
hash. I guess this is quite common.

My problem is that I'm not used (at all) with the 2005 edition (just
VC6) and the creator of the software uses extensively managed strings,
i.e.:

String ^ SomeName

And I'm in trouble converting this type to what SHA256Managed can
understand and then convert the hash back to "String^"

This is what I've found, just for testing the conversion techniques:
#include "stdafx.h"

using namespace System;
using namespace System::Text;
using namespace System::Security::Cryptography;

int main()
{
// Create two different encodings.
Encoding^ ascii = Encoding::ASCII;
Encoding^ unicode = Encoding::Unicode;

String ^ sSourceData = "operator";

// Perform the conversion from one encoding to the other.
array<unsigned char>^UnicodeData = unicode-
>GetBytes( sSourceData );
array<unsigned char>^AsciiData = Encoding::Convert( unicode,
ascii, UnicodeData );

SHA256^ shaM = gcnew SHA256Managed;
array<unsigned char>^ HashResult = shaM-
>ComputeHash( AsciiData );
Console::WriteLine(String::Format("Size: {0}", HashResult -
>Length));
array<Char>^asciiChars = gcnew array<Char>(ascii-
>GetCharCount( result, 0, HashResult ->Length ));
ascii->GetChars( result, 0, HashResult ->Length, asciiChars,
0 );
String ^ sOutputData = gcnew String( asciiChars );
Console::WriteLine( sOutputData );

}

As far as I could understand the ouput, it seems that ComputeHash is
generating real bytes and not hex digits, and I have read somewhere
(can't find it any more) that ComputeHash would generate a hex string
in the output array.

Any ideas?

Thanks in advance
Francisco
Dec 20 '07 #1
4 2208
vcnewbie wrote:
As far as I could understand the ouput, it seems that ComputeHash is
generating real bytes and not hex digits, and I have read somewhere
(can't find it any more) that ComputeHash would generate a hex string
in the output array.
Use System::Convert::ToBase64String to convert the array of bytes that you
get from the hash into a base64-encoded string.

-cd
Dec 20 '07 #2
On 20 dez, 13:34, "Carl Daniel [VC++ MVP]"
<cpdaniel_remove_this_and_nos...@mvps.org.nospamwr ote:
vcnewbie wrote:
As far as I could understand the ouput, it seems that ComputeHash is
generating real bytes and not hex digits, and I have read somewhere
(can't find it any more) that ComputeHash would generate a hex string
in the output array.

Use System::Convert::ToBase64String to convert the array of bytes that you
get from the hash into a base64-encoded string.

-cd
Thanks for the idea, it is a shortcut on what I am doing up to now.

But I guess I didn't put it clear: the SHA256 hash should be a string
of hex numbers, but its results are binary numbers, so I think I'll
have to make a conversion from binary to hex myself.

Thanks again
Francisco
Dec 20 '07 #3
Use System.BitConverter.ToString(byte[]) to do this. I converts an array of
bytes to hexadecimal string representation of the array.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

"vcnewbie" <fr****@gmail.comwrote in message
news:54**********************************@i12g2000 prf.googlegroups.com...
On 20 dez, 13:34, "Carl Daniel [VC++ MVP]"
<cpdaniel_remove_this_and_nos...@mvps.org.nospamwr ote:
>vcnewbie wrote:
As far as I could understand the ouput, it seems that ComputeHash is
generating real bytes and not hex digits, and I have read somewhere
(can't find it any more) that ComputeHash would generate a hex string
in the output array.

Use System::Convert::ToBase64String to convert the array of bytes that
you
get from the hash into a base64-encoded string.

-cd

Thanks for the idea, it is a shortcut on what I am doing up to now.

But I guess I didn't put it clear: the SHA256 hash should be a string
of hex numbers, but its results are binary numbers, so I think I'll
have to make a conversion from binary to hex myself.

Thanks again
Francisco

Dec 21 '07 #4
On 21 dez, 09:08, "Kevin Spencer" <unclechut...@nothinks.comwrote:
Use System.BitConverter.ToString(byte[]) to do this. I converts an array of
bytes to hexadecimal string representation of the array.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

Thanks a lot, Kevin, that's what I've been looking for, the
modifications on the original code are minimal and clear now.

The test code now is the following (in the case someone needs SHA256
hashing):
#include "stdafx.h"

using namespace System;
using namespace System::Text;

String ^ SHA256Hash(String ^ sInputData);

int main()
{
Console::WriteLine( "password1 : {0}" , SHA256Hash ( "password1
" ) ) ;
Console::WriteLine( "password2 : {0}" , SHA256Hash ( "password2
" ) ) ;
Console::WriteLine( "password3 : {0}" , SHA256Hash ( "password3
" ) ) ;
}

String ^ SHA256Hash(String ^ sInputData) {
// Create two different encodings.
Encoding^ ascii = Encoding::ASCII;
Encoding^ unicode = Encoding::Unicode;

// Initializes a SHA256 hash object for hash computation.
System::Security::Cryptography::SHA256^ shaM = gcnew
System::Security::Cryptography::SHA256Managed;

// Perform the conversion from one encoding to the other.
array<unsigned char>^UnicodeData = unicode->GetBytes( sInputData );
array<unsigned char>^AsciiData = Encoding::Convert( unicode, ascii,
UnicodeData );

// Computes the hash and converts it from binary to string, removing
unwanted characters.
array<unsigned char>^ BaseResult = shaM->ComputeHash( AsciiData );
String ^ sOutputData = System::BitConverter::ToString( BaseResult );
sOutputData = sOutputData->Replace("-","");

return sOutputData;
}
or, a little bit more obfuscated:
String ^ SHA256Hash(String ^ sInputData) {
//Create two different encodings and initializes a SHA256 hash object
for hash computation.
Encoding^ ascii = Encoding::ASCII;
Encoding^ unicode = Encoding::Unicode;
System::Security::Cryptography::SHA256^ shaM = gcnew
System::Security::Cryptography::SHA256Managed;

return (
System::BitConverter::ToString(
shaM->ComputeHash(
Encoding::Convert(
unicode,
ascii,
unicode->GetBytes( sInputData )
)
)
)->Replace("-","")
);
}
Dec 21 '07 #5

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

Similar topics

10
by: Marcin Kalicinski | last post by:
Why string literals are regarded as char * not as const char *? (1) void f(char *); (2) void f(const char *); f("foo") will call version (1) of function f. I understand that the exact type...
2
by: Thomas Matthews | last post by:
Hi, I'm working with Borland C++ Builder 6.2. My project uses the std::string class. However, Borland in its infinite wisdom has its own string class, AnsiString. To make my life easier, I...
12
by: ABeck | last post by:
Hello List, I have ar more or less academical question. Can there arise runtime errors in a program, if the include of <string.h> has been forgotten? If all the arguments to the functions of...
6
by: Marco Herrn | last post by:
Hi, I need to serialize an object into a string representation to store it into a database. So the SOAPFormatter seems to be the right formatter for this purpose. Now I have the problem that...
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
4
by: Russell Warren | last post by:
I've got a case where I want to convert binary blocks of data (various ctypes objects) to base64 strings. The conversion calls in the base64 module expect strings as input, so right now I'm...
10
by: =?Utf-8?B?RWxlbmE=?= | last post by:
I am surprised to discover that c# automatically converts an integer to a string when concatenating with the "+" operator. I thought c# was supposed to be very strict about types. Doesn't it seem...
5
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For...
3
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++...
10
by: Dancefire | last post by:
Hi, everyone, I'm writing a program using wstring(wchar_t) as internal string. The problem is raised when I convert the multibyte char set string with different encoding to wstring(which is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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.