Pad secret token correctly

After converting base 16 (hex) string of length 16 to integer and then to
base 36 we can end up with a shorter string due to implicit zeroes on
leading positions in the intermediate integer. This ensures
"00000000000000000000000000000000", "ffffffffffffffffffffffffffffffff"
and everything in between result in a string of length 25 after converting to
base 36.
private-asciicasts
Marcin Kulik 10 years ago
parent 2c0bfee0cd
commit 6bc2fd1048

@ -64,7 +64,7 @@ class Asciicast < ActiveRecord::Base
end
def self.generate_secret_token
SecureRandom.hex.to_i(16).to_s(36)
SecureRandom.hex.to_i(16).to_s(36).rjust(25, '0')
end
def stdout

@ -3,7 +3,7 @@ class AddSecretTokenToAsciicasts < ActiveRecord::Migration
add_column :asciicasts, :secret_token, :string
Asciicast.find_each do |asciicast|
asciicast.update_attribute(:secret_token, SecureRandom.hex.to_i(16).to_s(36))
asciicast.update_attribute(:secret_token, Asciicast.generate_secret_token)
end
change_column :asciicasts, :secret_token, :string, null: false

Loading…
Cancel
Save