https://github.com/bcrypt-ruby/bcrypt-ruby
bcrypt-ruby is a Ruby binding for the OpenBSD bcrypt() password hashing algorithm, allowing you to easily store a secure hash of your users' passwords.
https://github.com/bcrypt-ruby/bcrypt-ruby
Keywords from Contributors
activerecord activejob mvc rubygem rack sinatra rspec ruby-gem rubocop crash-reporting
Last synced: about 7 hours ago
JSON representation
Repository metadata
bcrypt-ruby is a Ruby binding for the OpenBSD bcrypt() password hashing algorithm, allowing you to easily store a secure hash of your users' passwords.
- Host: GitHub
- URL: https://github.com/bcrypt-ruby/bcrypt-ruby
- Owner: bcrypt-ruby
- License: other
- Created: 2008-05-07T23:29:27.000Z (over 17 years ago)
- Default Branch: master
- Last Pushed: 2024-10-29T23:10:47.000Z (about 1 year ago)
- Last Synced: 2025-12-19T21:22:47.994Z (20 days ago)
- Language: C
- Homepage:
- Size: 376 KB
- Stars: 1,964
- Watchers: 46
- Forks: 283
- Open Issues: 19
- Releases: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- License: COPYING
README.md
bcrypt-ruby
An easy way to keep your users' passwords secure.
Why you should use bcrypt()
If you store user passwords in the clear, then an attacker who steals a copy of your database has a giant list of emails
and passwords. Some of your users will only have one password -- for their email account, for their banking account, for
your application. A simple hack could escalate into massive identity theft.
It's your responsibility as a web developer to make your web application secure -- blaming your users for not being
security experts is not a professional response to risk.
bcrypt() allows you to easily harden your application against these kinds of attacks.
Note: JRuby versions of the bcrypt gem <= 2.1.3 had a security
vulnerability that
was fixed in >= 2.1.4. If you used a vulnerable version to hash
passwords with international characters in them, you will need to
re-hash those passwords. This vulnerability only affected the JRuby gem.
How to install bcrypt
gem install bcrypt
The bcrypt gem is available on the following Ruby platforms:
- JRuby
- RubyInstaller builds on Windows with the DevKit
- Any modern Ruby on a BSD/OS X/Linux system with a compiler
How to use bcrypt() in your Rails application
Note: Rails versions >= 3 ship with ActiveModel::SecurePassword which uses bcrypt-ruby.
has_secure_password docs
implements a similar authentication strategy to the code below.
The User model
require 'bcrypt'
class User < ActiveRecord::Base
# users.password_hash in the database is a :string
include BCrypt
def password
@password ||= Password.new(password_hash)
end
def password=(new_password)
@password = Password.create(new_password)
self.password_hash = @password
end
end
Creating an account
def create
@user = User.new(params[:user])
@user.password = params[:password]
@user.save!
end
Authenticating a user
def login
@user = User.find_by_email(params[:email])
if @user.password == params[:password]
give_token
else
redirect_to home_url
end
end
How to use bcrypt-ruby in general
require 'bcrypt'
my_password = BCrypt::Password.create("my password")
#=> "$2a$12$K0ByB.6YI2/OYrB4fQOYLe6Tv0datUVf6VZ/2Jzwm879BW5K1cHey"
my_password.version #=> "2a"
my_password.cost #=> 12
my_password == "my password" #=> true
my_password == "not my password" #=> false
my_password = BCrypt::Password.new("$2a$12$K0ByB.6YI2/OYrB4fQOYLe6Tv0datUVf6VZ/2Jzwm879BW5K1cHey")
my_password == "my password" #=> true
my_password == "not my password" #=> false
Check the rdocs for more details -- BCrypt, BCrypt::Password.
How bcrypt() works
bcrypt() is a hashing algorithm designed by Niels Provos and David Mazières of the OpenBSD Project.
Background
Hash algorithms take a chunk of data (e.g., your user's password) and create a "digital fingerprint," or hash, of it.
Because this process is not reversible, there's no way to go from the hash back to the password.
In other words:
hash(p) #=> <unique gibberish>
You can store the hash and check it against a hash made of a potentially valid password:
<unique gibberish> =? hash(just_entered_password)
Rainbow Tables
But even this has weaknesses -- attackers can just run lists of possible passwords through the same algorithm, store the
results in a big database, and then look up the passwords by their hash:
PrecomputedPassword.find_by_hash(<unique gibberish>).password #=> "secret1"
Salts
The solution to this is to add a small chunk of random data -- called a salt -- to the password before it's hashed:
hash(salt + p) #=> <really unique gibberish>
The salt is then stored along with the hash in the database, and used to check potentially valid passwords:
<really unique gibberish> =? hash(salt + just_entered_password)
bcrypt-ruby automatically handles the storage and generation of these salts for you.
Adding a salt means that an attacker has to have a gigantic database for each unique salt -- for a salt made of 4
letters, that's 456,976 different databases. Pretty much no one has that much storage space, so attackers try a
different, slower method -- throw a list of potential passwords at each individual password:
hash(salt + "aadvark") =? <really unique gibberish>
hash(salt + "abacus") =? <really unique gibberish>
etc.
This is much slower than the big database approach, but most hash algorithms are pretty quick -- and therein lies the
problem. Hash algorithms aren't usually designed to be slow, they're designed to turn gigabytes of data into secure
fingerprints as quickly as possible. bcrypt(), though, is designed to be computationally expensive:
Ten thousand iterations:
user system total real
md5 0.070000 0.000000 0.070000 ( 0.070415)
bcrypt 22.230000 0.080000 22.310000 ( 22.493822)
If an attacker was using Ruby to check each password, they could check ~140,000 passwords a second with MD5 but only
~450 passwords a second with bcrypt().
Cost Factors
In addition, bcrypt() allows you to increase the amount of work required to hash a password as computers get faster. Old
passwords will still work fine, but new passwords can keep up with the times.
The default cost factor used by bcrypt-ruby is 12, which is fine for session-based authentication. If you are using a
stateless authentication architecture (e.g., HTTP Basic Auth), you will want to lower the cost factor to reduce your
server load and keep your request times down. This will lower the security provided you, but there are few alternatives.
To change the default cost factor used by bcrypt-ruby, use BCrypt::Engine.cost = new_value:
BCrypt::Password.create('secret').cost
#=> 12, the default provided by bcrypt-ruby
# set a new default cost
BCrypt::Engine.cost = 8
BCrypt::Password.create('secret').cost
#=> 8
The default cost can be overridden as needed by passing an options hash with a different cost:
BCrypt::Password.create('secret', :cost => 6).cost #=> 6
More Information
bcrypt() is currently used as the default password storage hash in OpenBSD, widely regarded as the most secure operating
system available.
For a more technical explanation of the algorithm and its design criteria, please read Niels Provos and David Mazières'
Usenix99 paper:
https://www.usenix.org/events/usenix99/provos.html
If you'd like more down-to-earth advice regarding cryptography, I suggest reading Practical Cryptography by Niels
Ferguson and Bruce Schneier:
https://www.schneier.com/book-practical.html
Etc
- Author :: Coda Hale coda.hale@gmail.com
- Website :: https://codahale.com
Owner metadata
- Name: bcrypt-ruby
- Login: bcrypt-ruby
- Email:
- Kind: organization
- Description:
- Website:
- Location:
- Twitter:
- Company:
- Icon url: https://avatars.githubusercontent.com/u/81594302?v=4
- Repositories: 1
- Last ynced at: 2024-03-25T18:55:07.828Z
- Profile URL: https://github.com/bcrypt-ruby
GitHub Events
Total
- Issues event: 5
- Watch event: 48
- Issue comment event: 8
- Push event: 6
- Pull request review event: 2
- Pull request review comment event: 2
- Pull request event: 8
- Fork event: 3
Last Year
- Issues event: 3
- Watch event: 32
- Issue comment event: 6
- Push event: 3
- Pull request review event: 2
- Pull request review comment event: 2
- Pull request event: 2
- Fork event: 3
Committers metadata
Last synced: about 11 hours ago
Total Commits: 283
Total Committers: 59
Avg Commits per committer: 4.797
Development Distribution Score (DDS): 0.753
Commits in past year: 7
Committers in past year: 1
Avg Commits per committer in past year: 7.0
Development Distribution Score (DDS) in past year: 0.0
| Name | Commits | |
|---|---|---|
| T.J. Schuck | tj@g****m | 70 |
| Aaron Patterson | a****n@g****m | 43 |
| codahale | c****e@b****a | 31 |
| Coda Hale | c****e@g****m | 17 |
| Aman Gupta | a****n@t****t | 16 |
| Hongli Lai (Phusion) | h****i@p****l | 15 |
| Josh Buker | c****o@j****m | 12 |
| Nate Smith | n****e@t****m | 7 |
| Alan Savage | a****v@g****m | 6 |
| Felix | hi@l****e | 4 |
| Sergey Alekseev | s****y@a****o | 3 |
| Erik Michaels-Ober | s****k@g****m | 3 |
| Adam Grare | a****e@r****m | 3 |
| Brandon Fish | b****h@o****m | 2 |
| Bruno Henrique - Garu | s****o@g****m | 2 |
| Florin Onica | f****k@g****m | 2 |
| Jeremy Daer | j****r@g****m | 2 |
| Kenichi Kamiya | k****1@g****m | 2 |
| Paul Annesley | p****l@a****c | 2 |
| bfarago | b****o@g****m | 2 |
| Chad Jolly | c****y@l****m | 1 |
| Carter Brainerd (thecarterb) | c****7@g****m | 1 |
| Björn Esser | b****2@f****g | 1 |
| Benjamin Fleischer | g****b@b****m | 1 |
| Bart de Water | b****r@g****m | 1 |
| Adam Daniels | a****m@m****a | 1 |
| Charlie Savage | c****s@z****m | 1 |
| Griffin Smith | g****h@g****m | 1 |
| Ted Unangst | t****u@m****g | 1 |
| timcraft | m****l@t****m | 1 |
| and 29 more... | ||
Committer domains:
- linus-ux31e.(none): 1
- rapid7.com: 1
- joshpeek.com: 1
- atg.auto: 1
- mattwildig.co.uk: 1
- eventpower.com: 1
- boaventura.dev: 1
- vmware.com: 1
- layer22.com: 1
- remworks.net: 1
- deelstra.org: 1
- s21g.com: 1
- timcraft.com: 1
- mini.t3rl.org: 1
- getnomi.com: 1
- zerista.com: 1
- mediadrive.ca: 1
- benjaminfleischer.com: 1
- fedoraproject.org: 1
- lawgical.com: 1
- annesley.cc: 1
- oracle.com: 1
- redhat.com: 1
- asoft.co: 1
- l33t.name: 1
- theinternate.com: 1
- joshbuker.com: 1
- phusion.nl: 1
- tmm1.net: 1
- getharvest.com: 1
Issue and Pull Request metadata
Last synced: 8 days ago
Total issues: 54
Total pull requests: 70
Average time to close issues: about 1 year
Average time to close pull requests: over 1 year
Total issue authors: 49
Total pull request authors: 44
Average comments per issue: 4.74
Average comments per pull request: 2.71
Merged pull request: 46
Bot issues: 0
Bot pull requests: 0
Past year issues: 2
Past year pull requests: 3
Past year average time to close issues: N/A
Past year average time to close pull requests: N/A
Past year issue authors: 2
Past year pull request authors: 2
Past year average comments per issue: 2.0
Past year average comments per pull request: 0.67
Past year merged pull request: 0
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- mohamedhafez (4)
- joshbuker (2)
- boutil (2)
- daya (1)
- davestevens (1)
- iamhamzaawan (1)
- alecvn (1)
- esposito (1)
- mlen (1)
- ben-b-ow (1)
- hiiiide (1)
- fahad-afzal (1)
- copiousfreetime (1)
- larskanis (1)
- helosshi (1)
Top Pull Request Authors
- tenderlove (6)
- joshbuker (5)
- nwjsmith (4)
- sergey-alekseev (3)
- tjschuck (3)
- kachick (2)
- federicoaldunate (2)
- mohamedhafez (2)
- davestevens (2)
- jmartin-r7 (2)
- hakeem0114 (2)
- m-nakamura145 (2)
- jeremy (2)
- glittershark (2)
- olleolleolle (2)
Top Issue Labels
Top Pull Request Labels
Package metadata
- Total packages: 3
-
Total downloads:
- rubygems: 724,141,471 total
- Total docker downloads: 1,095,631,904
- Total dependent packages: 351 (may contain duplicates)
- Total dependent repositories: 288,069 (may contain duplicates)
- Total versions: 128
- Total maintainers: 4
gem.coop: bcrypt
bcrypt() is a sophisticated and secure hash algorithm designed by The OpenBSD project for hashing passwords. The bcrypt Ruby gem provides a simple wrapper for safely handling passwords.
- Homepage: https://github.com/bcrypt-ruby/bcrypt-ruby
- Documentation: http://www.rubydoc.info/gems/bcrypt/
- Licenses: MIT
- Latest release: 3.1.21 (published 8 days ago)
- Last Synced: 2026-01-08T16:00:48.221Z (about 13 hours ago)
- Versions: 54
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 362,254,960 Total
- Docker Downloads: 547,815,952
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 0.046%
- Downloads: 0.063%
- Docker downloads count: 0.121%
- Maintainers (4)
rubygems.org: bcrypt
bcrypt() is a sophisticated and secure hash algorithm designed by The OpenBSD project for hashing passwords. The bcrypt Ruby gem provides a simple wrapper for safely handling passwords.
- Homepage: https://github.com/bcrypt-ruby/bcrypt-ruby
- Documentation: http://www.rubydoc.info/gems/bcrypt/
- Licenses: MIT
- Latest release: 3.1.21 (published 8 days ago)
- Last Synced: 2026-01-06T22:34:24.062Z (2 days ago)
- Versions: 54
- Dependent Packages: 351
- Dependent Repositories: 288,069
- Downloads: 361,886,511 Total
- Docker Downloads: 547,815,952
-
Rankings:
- Downloads: 0.065%
- Dependent repos count: 0.085%
- Dependent packages count: 0.126%
- Docker downloads count: 0.223%
- Average: 0.54%
- Stargazers count: 1.184%
- Forks count: 1.559%
- Maintainers (4)
proxy.golang.org: github.com/bcrypt-ruby/bcrypt-ruby
- Homepage:
- Documentation: https://pkg.go.dev/github.com/bcrypt-ruby/bcrypt-ruby#section-documentation
- Licenses: other
- Latest release: v3.1.21+incompatible (published 8 days ago)
- Last Synced: 2026-01-06T03:41:30.854Z (3 days ago)
- Versions: 20
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Stargazers count: 1.539%
- Forks count: 1.674%
- Average: 5.897%
- Dependent packages count: 9.576%
- Dependent repos count: 10.802%
Dependencies
- actions/checkout v2 composite
- ruby/setup-ruby v1 composite
- rake-compiler ~> 1.2.0 development
- rspec >= 3 development
Score: 32.99204078073178