# Legacy encryption function

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

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [April 27, 2009, 5:03pm UTC](https://www.ruby-forum.com/t/legacy-encryption-function/166588/1 "2009-04-27T17:03:20Z")

</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:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [April 27, 2009, 5:23pm UTC](https://www.ruby-forum.com/t/legacy-encryption-function/166588/2 "2009-04-27T17:23:53Z")

</div>

On Apr 27, 4:02 pm, Alex [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> {
> 
> ```
> 
> }
> 
> Here is the Ruby encryption function:  
> …  
> require ‘digest/sha1’  
> Base64.encode64(Digest::SHA1.hexdigest(‘ruby’))

this outputs the digest as hex (before base64ing it), ie a string  
containing the characters “0” through “9”, “A” through “F”, whereas  
your java code outputs the raw data, ie if the first 2 bytes of the  
digest were 53 the ruby code would output

53

but the java code would output

S (the ascii code for S is 53).

Digest::SHA1.digest(‘ruby’) would be the same behaviour as your java  
code (don’t forget to convert the text to upper case)

Fred.

Fred

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [April 27, 2009, 5:49pm UTC](https://www.ruby-forum.com/t/legacy-encryption-function/166588/3 "2009-04-27T17:49:44Z")

</div>

Works. Thank you for your help!

On Apr 27, 11:23 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
