Search code examples
rubymd5salt-cryptography

ruby: create MD5 checksum with salt?


I'm trying to create an MD5 checksum in ruby with salt, but I cannot find any way to do this using the standard digest/md5 package.

I know I can do this:

require 'digest/md5'
checksum = '$1$' + (Digest::MD5.new << plaintext).to_s

However, there doesn't appear to be any way to specify salt for this MD5 checksum generation using digest, and I haven't found any other package I could use for this in ruby.

Is this even possible in ruby?


Solution

  • I found the following, and it does what I want ...

    https://github.com/mogest/unix-crypt

    It works like this:

    require 'unix_crypt'
    checksum = UnixCrypt::MD5.build(plaintext, salt)
    

    This generates the same checksum as is used in /etc/shadow, which is what I want to use it for,