Ruby 2.6.0 リファレンスマニュアル > ライブラリ一覧 > securerandomライブラリ > SecureRandomモジュール > urlsafe_base64

singleton method SecureRandom.urlsafe_base64

urlsafe_base64(n = nil, padding = false) -> String[permalink][rdoc]

ランダムで URL-safe な base64 文字列を生成して返します。

[PARAM] n:
文字列の生成に使われるランダムネスのサイズを整数で指定します。生成される文字列のサイズはn の約 4/3 倍になります。 nil を指定した場合 n として 16 が使われます。
[PARAM] padding:
真を指定すると '=' でパディングを行います。偽を指定するとパディングを行いません。デフォルトは偽です。
[EXCEPTION] NotImplementedError:
安全な乱数発生器が使えない場合に発生します。
require 'securerandom'
p SecureRandom.urlsafe_base64 #=> "b4GOKm4pOYU_-BOXcrUGDg"
p SecureRandom.urlsafe_base64 #=> "UZLdOkzop70Ddx-IJR0ABg"
p SecureRandom.urlsafe_base64(nil, true) #=> "i0XQ-7gglIsHGV2_BNPrdQ=="
p SecureRandom.urlsafe_base64(nil, true) #=> "-M8rLhr7JEpJlqFGUMmOxg=="

[SEE_ALSO] SecureRandom.base64, [RFC3548]