Class: SecurelyHashedAttributes::Coders::BCryptPasswordColumn

Inherits:
Object
  • Object
show all
Defined in:
lib/securely_hashed_attributes/coders/bcrypt_password_column.rb

Overview

This coder serializes strings to BCrypt::Password instances which, as the name suggests, are suitable handlers for passwords.

Constant Summary collapse

RESCUE_ERRORS =

A list of errors to handle when loading

[ArgumentError, BCrypt::Errors::InvalidHash]

Class Method Summary collapse

Class Method Details

.dump(str) ⇒ BCrypt::Password

Wrap the given data in the warm blanket of a BCrypt::Password

Parameters:

  • str (#to_s)

    data to serialize

Returns:

  • (BCrypt::Password)


10
11
12
# File 'lib/securely_hashed_attributes/coders/bcrypt_password_column.rb', line 10

def self.dump str
  BCrypt::Password.create str
end

.load(digest) ⇒ BCrypt::Password, String

Takes a String digest generated by bcrypt and wraps it in a new BCrypt::Password for functionality beyond that of a simple String. If digest is nil, an empty string is returned. If bcrypt does not recognize digest as a valid hash, digest itself is returned.

Parameters:

  • digest (String)

    a string digest generated by bcrypt

Returns:

  • (BCrypt::Password, String)


20
21
22
23
24
25
26
27
# File 'lib/securely_hashed_attributes/coders/bcrypt_password_column.rb', line 20

def self.load digest
  return '' if digest.nil?
  begin
    BCrypt::Password.new digest
  rescue *RESCUE_ERRORS
    digest
  end
end