Skip to content

Commit 9555541

Browse files
committed
Docs: crypto providers
1 parent c423467 commit 9555541

File tree

7 files changed

+67
-48
lines changed

7 files changed

+67
-48
lines changed

UPGRADING.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ Supplemental instructions to complement CHANGELOG.md.
44

55
## 3.4.0
66

7-
In version 3.4.0, the default crypto_provider was changed from *Sha512* to *SCrypt*.
7+
In version 3.4.0, released 2014-03-03, the default crypto_provider was changed
8+
from *Sha512* to *SCrypt*.
89

9-
If you never set a crypto_provider and are upgrading, your passwords will break unless you set the original:
10+
If you never set a crypto_provider and are upgrading, your passwords will break
11+
unless you specify `Sha512`.
1012

1113
``` ruby
1214
c.crypto_provider = Authlogic::CryptoProviders::Sha512

lib/authlogic.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
# Authlogic uses ActiveSupport's core extensions like `strip_heredoc`, which
2-
# ActiveRecord does not `require`. It's possible that we could save a few
3-
# milliseconds by loading only the specific core extensions we need, but
4-
# `all.rb` is simpler. We can revisit this decision if it becomes a problem.
1+
# Authlogic uses ActiveSupport's core extensions like `strip_heredoc` and
2+
# `squish`. ActiveRecord does not `require` these exensions, so we must.
3+
#
4+
# It's possible that we could save a few milliseconds by loading only the
5+
# specific core extensions we need, but `all.rb` is simpler. We can revisit this
6+
# decision if it becomes a problem.
57
require "active_support/all"
68

79
require "active_record"

lib/authlogic/acts_as_authentic/password.rb

+12-3
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,18 @@ def merge_validates_length_of_password_confirmation_field_options(options = {})
195195
validates_length_of_password_confirmation_field_options.merge(options)
196196
end
197197

198-
# The class you want to use to encrypt and verify your encrypted passwords. See
199-
# the Authlogic::CryptoProviders module for more info on the available methods and
200-
# how to create your own.
198+
# The class you want to use to encrypt and verify your encrypted
199+
# passwords. See the Authlogic::CryptoProviders module for more info on
200+
# the available methods and how to create your own.
201+
#
202+
# The family of adaptive hash functions (BCrypt, SCrypt, PBKDF2) is the
203+
# best choice for password storage today. We recommend SCrypt. Other
204+
# one-way functions like SHA512 are inferior, but widely used.
205+
# Reverisbile functions like AES256 are the worst choice.
206+
#
207+
# You can use the `transition_from_crypto_providers` option to gradually
208+
# transition to a better crypto provider without causing your users any
209+
# pain.
201210
#
202211
# * <tt>Default:</tt> CryptoProviders::SCrypt
203212
# * <tt>Accepts:</tt> Class

lib/authlogic/crypto_providers.rb

+21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
module Authlogic
2+
# The acts_as_authentic method has a crypto_provider option. This allows you
3+
# to use any type of encryption you like. Just create a class with a class
4+
# level encrypt and matches? method. See example below.
5+
#
6+
# === Example
7+
#
8+
# class MyAwesomeEncryptionMethod
9+
# def self.encrypt(*tokens)
10+
# # The tokens passed will be an array of objects, what type of object
11+
# # is irrelevant, just do what you need to do with them and return a
12+
# # single encrypted string. For example, you will most likely join all
13+
# # of the objects into a single string and then encrypt that string.
14+
# end
15+
#
16+
# def self.matches?(crypted, *tokens)
17+
# # Return true if the crypted string matches the tokens. Depending on
18+
# # your algorithm you might decrypt the string then compare it to the
19+
# # token, or you might encrypt the tokens and make sure it matches the
20+
# # crypted string, its up to you.
21+
# end
22+
# end
223
module CryptoProviders
324
autoload :MD5, "authlogic/crypto_providers/md5"
425
autoload :Sha1, "authlogic/crypto_providers/sha1"

lib/authlogic/crypto_providers/aes256.rb

+16-11
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
module Authlogic
44
module CryptoProviders
5-
# This encryption method is reversible if you have the supplied key. So in order to
6-
# use this encryption method you must supply it with a key first. In an initializer,
7-
# or before your application initializes, you should do the following:
5+
# This encryption method is reversible if you have the supplied key. So in
6+
# order to use this encryption method you must supply it with a key first.
7+
# In an initializer, or before your application initializes, you should do
8+
# the following:
89
#
910
# Authlogic::CryptoProviders::AES256.key = "long, unique, and random key"
1011
#
11-
# My final comment is that this is a strong encryption method, but its main weakness
12-
# is that it's reversible. If you do not need to reverse the hash then you should
13-
# consider Sha512 or BCrypt instead.
12+
# My final comment is that this is a strong encryption method, but its main
13+
# weakness is that it's reversible. If you do not need to reverse the hash
14+
# then you should consider Sha512 or BCrypt instead.
1415
#
15-
# Keep your key in a safe place, some even say the key should be stored on a separate
16-
# server. This won't hurt performance because the only time it will try and access the
17-
# key on the separate server is during initialization, which only happens once. The
18-
# reasoning behind this is if someone does compromise your server they won't have the
19-
# key also. Basically, you don't want to store the key with the lock.
16+
# Keep your key in a safe place, some even say the key should be stored on a
17+
# separate server. This won't hurt performance because the only time it will
18+
# try and access the key on the separate server is during initialization,
19+
# which only happens once. The reasoning behind this is if someone does
20+
# compromise your server they won't have the key also. Basically, you don't
21+
# want to store the key with the lock.
2022
class AES256
2123
class << self
2224
attr_writer :key
@@ -53,6 +55,9 @@ def aes
5355
# (https://github.com/ruby/openssl/commit/5c20a4c014) when openssl
5456
# became a gem. Its first release as a gem was 2.0.0, in ruby 2.4.
5557
# (See https://github.com/ruby/ruby/blob/v2_4_0/NEWS)
58+
#
59+
# When we eventually drop support for ruby < 2.4, we can probably also
60+
# drop support for openssl gem < 2.
5661
def openssl_cipher_class
5762
if ::Gem::Version.new(::OpenSSL::VERSION) < ::Gem::Version.new("2.0.0")
5863
::OpenSSL::Cipher::Cipher

lib/authlogic/crypto_providers/sha1.rb

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
module Authlogic
44
module CryptoProviders
5-
# This class was made for the users transitioning from restful_authentication. I
6-
# highly discourage using this crypto provider as it is far inferior to your other
7-
# options. Please use any other provider offered by Authlogic.
5+
# This class was made for the users transitioning from
6+
# restful_authentication. Use of this crypto provider is highly discouraged.
7+
# It is far inferior to your other options. Please use any other provider
8+
# offered by Authlogic.
89
class Sha1
910
class << self
1011
def join_token
1112
@join_token ||= "--"
1213
end
1314
attr_writer :join_token
1415

15-
# The number of times to loop through the encryption. This is ten because that is
16-
# what restful_authentication defaults to.
16+
# The number of times to loop through the encryption. This is ten
17+
# because that is what restful_authentication defaults to.
1718
def stretches
1819
@stretches ||= 10
1920
end
@@ -29,8 +30,8 @@ def encrypt(*tokens)
2930
digest
3031
end
3132

32-
# Does the crypted password match the tokens? Uses the same tokens that were used
33-
# to encrypt.
33+
# Does the crypted password match the tokens? Uses the same tokens that
34+
# were used to encrypt.
3435
def matches?(crypted, *tokens)
3536
encrypt(*tokens) == crypted
3637
end

lib/authlogic/crypto_providers/sha512.rb

-21
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,6 @@
11
require "digest/sha2"
22

33
module Authlogic
4-
# The acts_as_authentic method has a crypto_provider option. This allows you
5-
# to use any type of encryption you like. Just create a class with a class
6-
# level encrypt and matches? method. See example below.
7-
#
8-
# === Example
9-
#
10-
# class MyAwesomeEncryptionMethod
11-
# def self.encrypt(*tokens)
12-
# # The tokens passed will be an array of objects, what type of object
13-
# # is irrelevant, just do what you need to do with them and return a
14-
# # single encrypted string. For example, you will most likely join all
15-
# # of the objects into a single string and then encrypt that string.
16-
# end
17-
#
18-
# def self.matches?(crypted, *tokens)
19-
# # Return true if the crypted string matches the tokens. Depending on
20-
# # your algorithm you might decrypt the string then compare it to the
21-
# # token, or you might encrypt the tokens and make sure it matches the
22-
# # crypted string, its up to you.
23-
# end
24-
# end
254
module CryptoProviders
265
# = Sha512
276
#

0 commit comments

Comments
 (0)