Madogiwa Blog

主に技術系の学習メモに使っていきます。

Ruby on Rails: `rails secret`でセキュアな文字列を生成するMEMO

今までsecret_key_base等を設定するときにわざわざ以下のような感じでconsoleを起動してセキュアな文字列を取得していたのですが、

$ bin/rails console
irb(main):010:0> SecureRandom.hex(64)
=> "ad56c1c986e397149281da99f6e268d4e3657d65a46600dcaeccd8a368655b141f23c0a4791466cd9d51552d2b1a3b78ec21bfdf67c94d77321ad065806b6bcb"

bin/rails secretで簡単に取得出来るのを知らなかったのでメモ📝

rails secretでセキュアな文字列を取得する

やりかたは簡単で以下のコマンドを実行するとセキュアな文字列が標準出力されます。

$ bin/rails secret
dc83908ca804b051809c1b565d20582d57ab77fae9dad8eb8935b36eba2e2437c28d8abd43d862d797989b9a58340b6776fe3441ed1c3d02c5d87ac7fe2a98fe

Railsガイドでも推奨されているようですね!

秘密鍵は十分に長く、かつランダムなものにしなければなりません。一意な秘密鍵を得るにはrails secretを使います。

Rails セキュリティガイド - Railsガイド

ちなみにSecureRandom.hex(64)を実行しているようです。

github.com

SecureRandom.hex(64)は128文字のHEX文字列(0~9,a~f)を出力するので以下の画像のLowercase Lettersに該当しそうですが128文字あれば安全そうですね。

f:id:madogiwa0124:20211002164740p:plain [OC] I hope you find this one more beautiful than the last - updated table on time to brute force passwords : dataisbeautiful

参考

saku.hatenadiary.com