Module: MacAdmin::Password

Extended by:
Password
Included in:
Password, ShadowHash
Defined in:
lib/macadmin/password.rb,
ext/macadmin/password/crypto.c

Overview

Password

  • module containing methods for converting a plain String into Mac OS X password hash

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.salted_sha512_pbkdf2_from_stringObject



20
# File 'ext/macadmin/password/crypto.c', line 20

static VALUE salted_sha512_pbkdf2_from_string(VALUE self, VALUE input);

Instance Method Details

#apropos(password) ⇒ Object

Create a platform appropriate password

  • single param: String

  • returns: SaltedSHA512PBKDF2 or SaltedSHA512 or SaltedSHA1 depending on platform



59
60
61
62
63
64
65
66
67
68
# File 'lib/macadmin/password.rb', line 59

def apropos(password)
  platform = MacAdmin::Common::MAC_OS_X_PRODUCT_VERSION
  if platform >= 10.8
    return salted_sha512_pbkdf2 password
  elsif platform == 10.7
    return salted_sha512 password
  else
    return salted_sha1 password
  end
end

#convert_to_blob(hex) ⇒ Object

Convert hex string to CFBlob



18
19
20
21
# File 'lib/macadmin/password.rb', line 18

def convert_to_blob(hex)
  ascii = hex.scan(/../).collect { |byte| byte.hex.chr }.join
  CFPropertyList::Blob.new(ascii)
end

#convert_to_hex(string) ⇒ Object

Convert ASCII string to hex bytes



13
14
15
# File 'lib/macadmin/password.rb', line 13

def convert_to_hex(string)
  string.unpack('H*').first
end

#salted_sha1(password) ⇒ Object

Creates a SaltedSHA1 password from String

  • single param: String

  • returns: SaltedSHA1



50
51
52
53
54
# File 'lib/macadmin/password.rb', line 50

def salted_sha1(password)
  salt = SecureRandom.random_bytes(4)
  hash = Digest::SHA1.hexdigest(salt + password)
  SaltedSHA1.new((convert_to_hex(salt) + hash).upcase)
end

#salted_sha512(password) ⇒ Object

Creates a SaltedSHA512 password from String

  • single param: String

  • returns: SaltedSHA512



41
42
43
44
45
# File 'lib/macadmin/password.rb', line 41

def salted_sha512(password)
  salt = SecureRandom.random_bytes(4)
  hash = Digest::SHA512.hexdigest(salt + password)
  SaltedSHA512.new(convert_to_hex(salt) + hash)
end

#salted_sha512_pbkdf2(password) ⇒ Object

Creates a SaltedSHA512PBKDF2 password from String

  • single param: String

  • returns: SaltedSHA512PBKDF2



31
32
33
34
# File 'lib/macadmin/password.rb', line 31

def salted_sha512_pbkdf2(password)
  hash = salted_sha512_pbkdf2_from_string password
  SaltedSHA512PBKDF2.new hash
end