Created
May 1, 2009 10:39
-
-
Save kennethkalmer/104980 to your computer and use it in GitHub Desktop.
Generating shadow passwords with Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Linux | |
class User | |
class << self | |
# Generate an MD5 salt string | |
def salt | |
seeds = ('a'..'z').to_a | |
seeds.concat( ('A'..'Z').to_a ) | |
seeds.concat( (0..9).to_a ) | |
seeds.concat ['/', '.'] | |
seeds.compact! | |
salt_string = '$1$' | |
8.times { salt_string << seeds[ rand(seeds.size) ].to_s } | |
salt_string | |
end | |
# Crypt a password suitable for use in shadow files | |
def crypt( string ) | |
string.crypt( self.salt ) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.dirname(__FILE__) + '/../spec_helper' | |
describe Linux::User do | |
describe "generating shadow passwords" do | |
it "should generate a salt for crypt" do | |
salt = Linux::User.salt | |
salt.length.should be(11) | |
salt.should match(/^\$1\$[a-zA-Z0-9\.\/]{8}$/) | |
end | |
it "should generate a shadow password" do | |
pass = Linux::User.crypt( 'secret' ) | |
pass.should match(/^\$1\$[a-zA-Z0-9\.\/]{8}\$[a-zA-Z0-9\.\/]{22}$/) | |
pass.length.should be(34) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment