# Legacy encryption function

**URL:** <https://www.ruby-forum.com/t/legacy-encryption-function/166592>\
**Category:** Ruby\
**Created:** [April 27, 2009, 5:19pm UTC](https://www.ruby-forum.com/t/legacy-encryption-function/166592 "2009-04-27T17:19:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [April 27, 2009, 5:19pm UTC](https://www.ruby-forum.com/t/legacy-encryption-function/166592/1 "2009-04-27T17:19:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [April 27, 2009, 5:52pm UTC](https://www.ruby-forum.com/t/legacy-encryption-function/166592/2 "2009-04-27T17:52:05Z")

</div>

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

> {  
> return new BASE64Encoder().encode(raw);  
> Now here is the resulting String calling the Java function with the  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

What happens if you do this:

mDigest = MessageDigest.getInstance(“SHA-1”);

Cheers–

## Charles

Charles J.  
Advanced Computing Center for Research and Education  
Vanderbilt University

---

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [April 27, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/legacy-encryption-function/166592/3 "2009-04-27T18:00:55Z")

</div>

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

> Vanderbilt University  
> Stupid me. SHA probably _is_ sha-1 in the Java world. ☹

Cheers–

## Charles

Charles J.  
Advanced Computing Center for Research and Education  
Vanderbilt University

---

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [April 27, 2009, 7:07pm UTC](https://www.ruby-forum.com/t/legacy-encryption-function/166592/4 "2009-04-27T19:07:34Z")

</div>

Charles J. wrote:

> On Apr 27, 2009, at 10:51 AM, Charles J. wrote:
> 
> > Vanderbilt University  
> > Stupid me. SHA probably _is_ sha-1 in the Java world. ☹
> 
> Cheers–
> 
> ## Charles
> 
> Charles J.  
> 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!
