0
0

More than 3 years have passed since last update.

devise のパスワードカラム名は「encrypted_pa​​ssword」だけど不可逆

Last updated at Posted at 2020-10-12

「ハッシュ化」と「暗号化」

今まで

ハッシュ化 → 元に戻せないやつ。英語だと hash。
暗号化 → 元に戻せるやつ。英語だと encrypt。

だと思って生きてきました。
今でもそう思っています。

ところが、、、

devise のパスワードカラム名・・・encrypted_pa​​ssword!?

先日ふと気づいたのですが、gem devise のパスワードカラムの名前が「encrypted_pa​​ssword」になってます。

えっ、これってもしかして復号できちゃうの??

実装を見てみる

普通にハッシュ化してるっぽい。

/lib/devise/models/database_authenticatable.rb
      def password=(new_password)
        @password = new_password
        self.encrypted_password = password_digest(@password) if @password.present?
      end

      ...

      def password_digest(password)
        Devise::Encryptor.digest(self.class, password)
      end
/lib/devise/encryptor.rb
require 'bcrypt'

module Devise
  module Encryptor
    def self.digest(klass, password)
      if klass.pepper.present?
        password = "#{password}#{klass.pepper}"
      end
      ::BCrypt::Password.create(password, cost: klass.stretches).to_s
    end

    ...

まとめ

カラム名は「encrypted_pa​​ssword」だけどやってることはハッシュ化(不可逆)だから安心していい。

--

ちなみに、過去にカラム名をリネームする案も出たことがあるようですが、
「既存の利用者に与える影響でかすぎ。名前変更のリスクに見合うメリット無い(超意訳)」
との理由により close されていました。

The word 'encrypted_password' raises red flags; time to rename?
https://github.com/heartcombo/devise/issues/5025

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0