Sha256: 3a854a478d85fb6d3a124f4e34fd2e5c9851c91535fbeb1a54c2cb66f4dfa8bd

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

# (c) 2017 Ribose Inc.

require 'ffi'

require 'botan/rng'
require 'botan/utils'

module Botan
  # bcrypt password hashing
  #
  # == Examples
  # === examples/bcrypt.rb
  # {include:file:examples/bcrypt.rb}
  #
  module BCrypt
    # Generates a password hash using bcrypt.
    #
    # @param password [String] the password to hash
    # @param work_factor [Integer] the bcrypt work factor
    # @param rng [Botan::RNG] the RNG to use
    # @return [String] the generated password hash
    def self.hash(password, work_factor: 10, rng: Botan::RNG.new)
      out_len = 64
      out_buf = FFI::MemoryPointer.new(:uint8, out_len)
      flags = 0
      out_len_ptr = FFI::MemoryPointer.new(:size_t)
      out_len_ptr.write(:size_t, out_len)
      Botan.call_ffi(:botan_bcrypt_generate,
                     out_buf, out_len_ptr,
                     password, rng.ptr, work_factor, flags)
      result = out_buf.read_bytes(out_len_ptr.read(:size_t))
      result = result[0...-1] if result[-1] == "\x00"
      result
    end

    # Checks a password against a bcrypt hash.
    #
    # @param password [String] the password to hash
    # @param phash [String] the bcrypt hash
    # @return [Boolean] true if the provided password is correct
    def self.valid?(password:, phash:)
      rc = Botan.call_ffi_rc(:botan_bcrypt_is_valid, password, phash)
      rc.zero?
    end
  end # module
end # module

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
botan-0.1.2 lib/botan/bcrypt.rb
botan-0.1.0 lib/botan/bcrypt.rb