Skip to content

Commit

Permalink
Include password base encryption, and random password
Browse files Browse the repository at this point in the history
  • Loading branch information
bbuendia committed Nov 13, 2018
1 parent 5ab16e3 commit da72976
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ group :development, :test do
gem 'guard-rubocop'
gem 'listen', '3.0.8'
gem 'terminal-notifier-guard'
gem 'openssl', '2.1.2'
end
28 changes: 28 additions & 0 deletions lib/crypto/PBE.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

require 'openssl'
require 'securerandom'

module Crypto
class PBE
alpha_num = ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a
specials = ['-', '_', '+', '=', '#', '&', '*', '.']
CHARS = alpha_num + specials

def initialize(i = 1000)
@iterations = i
@digest = OpenSSL::Digest::SHA256.new
end

def random_password(length=32)
CHARS.sort_by { SecureRandom.random_number }.join[0...length]
end

def random_salt(size = 16)
SecureRandom.random_bytes(size)
end

def derived_key(password, salt, sizeKey = 24)
OpenSSL::PKCS5.pbkdf2_hmac(password, salt, @iterations, sizeKey, @digest)
end
end
end

0 comments on commit da72976

Please sign in to comment.