Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8376587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:27:51+00:00 2026-06-09T15:27:51+00:00

I was trying out the bcrypt-ruby gem and i wrote the following code to

  • 0

I was trying out the bcrypt-ruby gem and i wrote the following code to generate a random password and verify it

require 'bcrypt'
require 'securerandom'

def encrypt_token(tok)
    BCrypt::Password.create(tok)
end

def check_token(enc,tok)

    g = BCrypt::Password.new(enc)

    if tok==g
       puts 'equal'
    else
       puts 'not equal'
    end 
end

s = SecureRandom.hex(12)

puts s

e = encrypt_token(s)

puts e

check_token(e,s)

The code keeps printing ‘not equal’ instead of ‘equal’. Where am I going wrong? Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:27:53+00:00Added an answer on June 9, 2026 at 3:27 pm

    bcrypt has an automatic-salt feature. You can’t compare two bcrypts of the same string, they’ll be different.

    Try to compare like this :

    def check_token(enc,tok)
    
      if enc == tok #We compare it with the unencrypted string.
        puts 'equal'
      else
        puts 'not equal'
      end 
    
    end
    

    The trick is that when creating a new bcrypt, you end up with a Password object that overrides the == operator. It’ll check if the password is correct against an unencrypted string.

    Also because of this, be careful : in the example above, comparing enc == tok works.
    Comparing tok == enc won’t as you’ll be using the standard == from the class String

    Take a look at the doc and the source here :
    http://bcrypt-ruby.rubyforge.org/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying out some code on Ubuntu. I'm trying to run the following
Trying out the following, will result in error. How do I retrieve the values
Am trying out Ruby on Rails in Windows XP using the Hello World example.
Am trying out structuremap for the first time and am getting the following compiler
I have been trying out EF 4.1 ( code first ) with MVC 3.
I'm trying out very simple search popup on the JqGrid. Please see the code
I trying out some problems in Google's Code Jam. For the question Store Credit
Trying out PDO for the first time. $dbh = new PDO(mysql:host=$hostname;dbname=animals, $username, $password); $stmt
I'm trying out the following query: SELECT A,B,C FROM table WHERE field LIKE 'query%'
I trying out Django's class based views (CBVs). class BlahView(TemplateView): template_name = 'blah/blah.html' def

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.