|
15 | 15 | The first argument to this function is the password to hash. If it is |
16 | 16 | undef or an empty string, this function returns undef. |
17 | 17 |
|
18 | | - The second argument to this function is which type of hash to use. It |
| 18 | + The second argument to this function is which hash algorithm to use. It |
19 | 19 | will be converted into the appropriate crypt(3) hash specifier. Valid |
20 | 20 | hash types are: |
21 | 21 |
|
22 | | - |Hash type |Specifier| |
23 | | - |---------------------|---------| |
24 | | - |MD5 |1 | |
25 | | - |SHA-256 |5 | |
26 | | - |SHA-512 (recommended)|6 | |
| 22 | + |Hash type|Prefix|Note | |
| 23 | + |---------|------|---------------------| |
| 24 | + |MD5 |1 | | |
| 25 | + |SHA-256 |5 | | |
| 26 | + |SHA-512 |6 |Recommended | |
| 27 | + |bcrypt |2b | | |
| 28 | + |bcrypt-a |2a |bug compatible | |
| 29 | + |bcrypt-x |2x |bug compatible | |
| 30 | + |bcrypt-y |2y |historic alias for 2b| |
27 | 31 |
|
28 | 32 | The third argument to this function is the salt to use. |
29 | 33 |
|
30 | | - @return [Hash] |
31 | | - Provides a hash usable on most POSIX systems. |
| 34 | + @return [String] |
| 35 | + Provides a crypt hash usable on most POSIX systems. |
32 | 36 |
|
33 | 37 | > *Note:*: this uses the Puppet Server's implementation of crypt(3). If your |
34 | 38 | environment contains several different operating systems, ensure that they |
|
43 | 47 | arg |
44 | 48 | end |
45 | 49 | end |
| 50 | + |
| 51 | + hashes = { |
| 52 | + 'md5' => { prefix: '1' }, |
| 53 | + 'sha-256' => { prefix: '5' }, |
| 54 | + 'sha-512' => { prefix: '6' }, |
| 55 | + 'bcrypt' => { prefix: '2b', salt: %r{^[0-9]{2}\$[./A-Za-z0-9]{22}} }, |
| 56 | + 'bcrypt-a' => { prefix: '2a', salt: %r{^[0-9]{2}\$[./A-Za-z0-9]{22}} }, |
| 57 | + 'bcrypt-x' => { prefix: '2x', salt: %r{^[0-9]{2}\$[./A-Za-z0-9]{22}} }, |
| 58 | + 'bcrypt-y' => { prefix: '2y', salt: %r{^[0-9]{2}\$[./A-Za-z0-9]{22}} }, |
| 59 | + } |
| 60 | + |
46 | 61 | raise ArgumentError, 'pw_hash(): first argument must be a string' unless args[0].is_a?(String) || args[0].nil? |
47 | 62 | raise ArgumentError, 'pw_hash(): second argument must be a string' unless args[1].is_a? String |
48 | | - hashes = { 'md5' => '1', |
49 | | - 'sha-256' => '5', |
50 | | - 'sha-512' => '6' } |
51 | 63 | hash_type = hashes[args[1].downcase] |
52 | 64 | raise ArgumentError, "pw_hash(): #{args[1]} is not a valid hash type" if hash_type.nil? |
53 | 65 | raise ArgumentError, 'pw_hash(): third argument must be a string' unless args[2].is_a? String |
54 | 66 | raise ArgumentError, 'pw_hash(): third argument must not be empty' if args[2].empty? |
55 | | - raise ArgumentError, 'pw_hash(): characters in salt must be in the set [a-zA-Z0-9./]' unless %r{\A[a-zA-Z0-9./]+\z}.match?(args[2]) |
| 67 | + salt_doc = hash_type.include?(:salt) ? "match #{hash_type[:salt]}" : 'be in the set [a-zA-Z0-9./]' |
| 68 | + salt_regex = hash_type.fetch(:salt, %r{\A[a-zA-Z0-9./]+\z}) |
| 69 | + raise ArgumentError, "pw_hash(): characters in salt must #{salt_doc}" unless salt_regex.match?(args[2]) |
56 | 70 |
|
57 | 71 | password = args[0] |
58 | 72 | return nil if password.nil? || password.empty? |
59 | 73 |
|
60 | | - salt = "$#{hash_type}$#{args[2]}" |
| 74 | + salt = "$#{hash_type[:prefix]}$#{args[2]}" |
61 | 75 |
|
62 | 76 | # handle weak implementations of String#crypt |
63 | 77 | # dup the string to get rid of frozen status for testing |
|
0 commit comments