Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Ruby on Railsアプリケーションでパスワードのソルトを生成しようとしたときに、SecureRandom#hexメソッドに出くわしました。長さパラメータを2倍にする/返される文字列の長さが偶数であると主張するのはなぜですか?
このメソッドは、 nバイトのランダムシーケンスを生成し(random_bytesメソッドを参照)、そのシーケンスの基数16の表現(1バイトあたり2桁の16進数)を返します。
random_bytes
base64これが、 andメソッドがおよそ4 n / 3urlsafe_base64の長さの文字列を返す理由でもあります。nバイトを生成してから、Base-64エンコーディングを実行します。
base64
urlsafe_base64