[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Legacy encryption function...

Alexandre Alex

4/27/2009 3:19:00 PM

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?
--
Posted via http://www.ruby-....

3 Answers

cnjohnson

4/27/2009 3:52:00 PM

0


On Apr 27, 2009, at 10:19 AM, Alexandre Alex wrote:

> 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?
> --
> Posted via http://www.ruby-....
>
What happens if you do this:

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

Cheers--

Charles
---
Charles Johnson
Advanced Computing Center for Research and Education
Vanderbilt University





cnjohnson

4/27/2009 4:00:00 PM

0


On Apr 27, 2009, at 10:51 AM, Charles Johnson wrote:

> What happens if you do this:
>
> mDigest = MessageDigest.getInstance("SHA-1");
>
> Cheers--
>
> Charles
> ---
> Charles Johnson
> Advanced Computing Center for Research and Education
> Vanderbilt University
Stupid me. SHA probably *is* sha-1 in the Java world. :(

Cheers--

Charles
---
Charles Johnson
Advanced Computing Center for Research and Education
Vanderbilt University


Alexandre Alex

4/27/2009 5:08:00 PM

0

Charles Johnson wrote:
> On Apr 27, 2009, at 10:51 AM, Charles Johnson wrote:
>
>> Vanderbilt University
> Stupid me. SHA probably *is* sha-1 in the Java world. :(
>
> Cheers--
>
> Charles
> ---
> Charles Johnson
> Advanced Computing Center for Research and Education
> Vanderbilt University

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

Thanks anyway!
--
Posted via http://www.ruby-....