module Crypt

Constants

ALGORITHM

Public Instance Methods

base64(str) click to toggle source
# File lib/common/crypt.rb, line 21
def base64 str
  Base64.urlsafe_encode64(str)
end
bcrypt(plain, check=nil) click to toggle source
# File lib/common/crypt.rb, line 37
def bcrypt plain, check=nil
  if check
    BCrypt::Password.new(check) == [plain, secret].join('')
  else
    BCrypt::Password.create(plain + secret)
  end
end
decrypt(token, opts={}) click to toggle source

Crypt.decrypt('secret') Crypt.decrypt('secret', password:'pa$$w0rd')

# File lib/common/crypt.rb, line 57
def decrypt token, opts={}
  opts = opts.to_opts(:password, :ttl)

  token_data = JWT.decode token, secret+opts.password.to_s, true, { :algorithm => ALGORITHM }
  data = token_data[0]

  raise "Crypted data expired before #{Time.now.to_i - data['ttl']} seconds" if data['ttl'] && data['ttl'] < Time.now.to_i

  data['data']
end
encrypt(data, opts={}) click to toggle source

Crypt.encrypt('secret') Crypt.encrypt('secret', ttl:1.hour, password:'pa$$w0rd')

# File lib/common/crypt.rb, line 47
def encrypt data, opts={}
  opts          = opts.to_opts(:ttl, :password)
  payload       = { data:data }
  payload[:ttl] = Time.now.to_i + opts.ttl.to_i if opts.ttl

  JWT.encode payload, secret+opts.password.to_s, ALGORITHM
end
md5(str) click to toggle source
# File lib/common/crypt.rb, line 33
def md5 str
  Digest::MD5.hexdigest(str.to_s + secret)
end
secret() click to toggle source
# File lib/common/crypt.rb, line 17
def secret
  ENV.fetch('SECRET') { Lux.config.secret } || die('Lux.config.secret not set')
end
sha1(str) click to toggle source
# File lib/common/crypt.rb, line 29
def sha1 str
  Digest::SHA1.hexdigest(str.to_s + secret)
end
uid() click to toggle source
# File lib/common/crypt.rb, line 25
def uid
  SecureRandom.hex
end