Sha256: feeb283de7e5dfac01199bd2b5ba4f5472989faf37e28d51ced675b33cb8feb2

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

# @author: Dino Reic
# @description:
#   module for easy and convenient access to frequently used crypt operations

require 'openssl'
require 'base64'
require 'digest/md5'
require 'securerandom'

module Crypt
  extend self

  ALGORITHM = 'HS512'

  def secret
    ENV.fetch('SECRET') { Lux.config.secret } || die('Lux.config.secret not set')
  end

  def base64 str
    Base64.urlsafe_encode64(str)
  end

  def uid
    SecureRandom.hex
  end

  def sha1 str
    Digest::SHA1.hexdigest(str.to_s + secret)
  end

  def md5 str
    Digest::MD5.hexdigest(str.to_s + secret)
  end

  def bcrypt plain, check=nil
    if check
      BCrypt::Password.new(check) == [plain, secret].join('')
    else
      BCrypt::Password.create(plain + secret)
    end
  end

  # Crypt.encrypt('secret')
  # Crypt.encrypt('secret', ttl:1.hour, password:'pa$$w0rd')
  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

  # Crypt.decrypt('secret')
  # Crypt.decrypt('secret', password:'pa$$w0rd')
  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

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lux-fw-0.5.37 ./lib/common/crypt.rb
lux-fw-0.5.36 ./lib/common/crypt.rb
lux-fw-0.5.35 ./lib/common/crypt.rb
lux-fw-0.5.34 ./lib/common/crypt.rb
lux-fw-0.5.33 ./lib/common/crypt.rb
lux-fw-0.5.32 ./lib/common/crypt.rb