Forum: Ruby on Rails Legacy encryption function...

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Alex (Guest)
on 2009-04-27 17:03
(Received via mailing list)
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

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

    } catch (NoSuchAlgorithmException nsae) {
    } catch (UnsupportedEncodingException uee) {
    byte[] raw = mDigest.digest();
    return new BASE64Encoder().encode(raw);

Here is the Ruby encryption function:
require 'digest/sha1'

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

Here is what I get with my Ruby function for the same string:

So, anybody knows what I'm doing wrong?
Frederick Cheung (Guest)
on 2009-04-27 17:23
(Received via mailing list)
On Apr 27, 4:02 pm, Alex <> 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


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)


Alex (Guest)
on 2009-04-27 17:49
(Received via mailing list)
Works. Thank you for your help!

On Apr 27, 11:23 am, Frederick Cheung <>
This topic is locked and can not be replied to.