Class: MacAdmin::SaltedSHA512

Inherits:
ShadowHash show all
Defined in:
lib/macadmin/shadowhash.rb

Overview

Lion ShadowHashs

  • Mac OS X 10.7 store passwords as Salted SHA512 hashes

  • hash is stored directly in the user’s plist

Constant Summary collapse

LABEL =
'SALTED-SHA512'

Instance Attribute Summary collapse

Attributes inherited from ShadowHash

#label

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ShadowHash

create_from_user_record, read_shadowhashdata

Methods included from Password

#apropos, #convert_to_blob, #convert_to_hex, #salted_sha1, #salted_sha512, #salted_sha512_pbkdf2, salted_sha512_pbkdf2_from_string

Constructor Details

#initialize(string) ⇒ SaltedSHA512

Initializes a SaltedSHA512 ShadowHash object from string

  • string param should be a hex string, 68 bytes



161
162
163
164
# File 'lib/macadmin/shadowhash.rb', line 161

def initialize(string)
  @label = LABEL
  @hash = validate(string)
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



145
146
147
# File 'lib/macadmin/shadowhash.rb', line 145

def hash
  @hash
end

Class Method Details

.create_from_shadowhashdata(data) ⇒ Object

Constructs a SaltedSHA512 ShadowHash object from ShadowHashData

  • param is raw ShadowHashData object



151
152
153
154
155
# File 'lib/macadmin/shadowhash.rb', line 151

def create_from_shadowhashdata(data)
  value = data[SaltedSHA512::LABEL].to_s
  hex = MacAdmin::Password.convert_to_hex(value)
  self.new(hex)
end

Instance Method Details

#dataObject

Return the ShadowHash as a ShadowHashData object

  • Binary Plist



176
177
178
# File 'lib/macadmin/shadowhash.rb', line 176

def data
  @data ||= { @label => convert_to_blob(@hash) }.to_plist
end

#passwordObject

Return a Hash representation of the ShadowHash data



181
182
183
# File 'lib/macadmin/shadowhash.rb', line 181

def password
  { @label => @hash }
end

#validate(string) ⇒ Object

Validates the string param

  • ensure the string param is hex string 68 bytes long

Raises:

  • (ArgumentError)


168
169
170
171
172
# File 'lib/macadmin/shadowhash.rb', line 168

def validate(string)
  error = "Invalid: arg must be hexadecimal string (68 bytes)"
  raise ArgumentError.new(error) unless string =~ /([a-f0-9]{2}){68}/
  string
end