Legacy encryption function...

A

Alexandre Alex

Hello everyone, I have an old Java authentication web service that I
must use in my rails development. The service takes a username and an
encrypted password. I'm having trouble reproducing the same encryption
algorithm.

Here is the Java encryption function:
public static String digest(String text)
{
MessageDigest mDigest = null;
try
{
mDigest = MessageDigest.getInstance("SHA");


mDigest.update(text.toUpperCase().getBytes("UTF-8"));
} catch (NoSuchAlgorithmException nsae) {
nsae.printStackTrace();
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
}
byte[] raw = mDigest.digest();
return new BASE64Encoder().encode(raw);
}


Here is the Ruby encryption function:
...
require 'digest/sha1'
Base64.encode64(Digest::SHA1.hexdigest('ruby'))
...


Now here is the resulting String calling the Java function with the
string 'ruby':
"oJtN1kHfQdCCLFN7ATWgAxIH6bc="


Here is what I get with my Ruby function for the same string:
"MThlNDBlMTQwMWVlZjY3ZTFhZTY5ZWZhYjA5YWZiNzFmODdmZmI4MQ==\n"


So, anybody knows what I'm doing wrong?
 
C

Charles Johnson

Hello everyone, I have an old Java authentication web service that I
must use in my rails development. The service takes a username and an
encrypted password. I'm having trouble reproducing the same encryption
algorithm.

Here is the Java encryption function:
public static String digest(String text)
{
MessageDigest mDigest = null;
try
{
mDigest = MessageDigest.getInstance("SHA");


mDigest.update(text.toUpperCase().getBytes("UTF-8"));
} catch (NoSuchAlgorithmException nsae) {
nsae.printStackTrace();
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
}
byte[] raw = mDigest.digest();
return new BASE64Encoder().encode(raw);
}


Here is the Ruby encryption function:
...
require 'digest/sha1'
Base64.encode64(Digest::SHA1.hexdigest('ruby'))
...


Now here is the resulting String calling the Java function with the
string 'ruby':
"oJtN1kHfQdCCLFN7ATWgAxIH6bc="


Here is what I get with my Ruby function for the same string:
"MThlNDBlMTQwMWVlZjY3ZTFhZTY5ZWZhYjA5YWZiNzFmODdmZmI4MQ==\n"


So, anybody knows what I'm doing wrong?
What happens if you do this:

mDigest = MessageDigest.getInstance("SHA-1");

Cheers--

Charles
 
C

Charles Johnson

What happens if you do this:

mDigest = MessageDigest.getInstance("SHA-1");

Cheers--

Charles
Stupid me. SHA probably *is* sha-1 in the Java world. :(

Cheers--

Charles
 
A

Alex Folgueras

Charles said:
Stupid me. SHA probably *is* sha-1 in the Java world. :(

Cheers--

Charles

No problem, in fact I found my problem. I have to use the digest
function instead of hexdigest...

Thanks anyway!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top