• Stars
    star
    125
  • Rank 286,335 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Underlock makes it dead simple to encrypt and decrypt your data and files. It comes with little to no dependencies and has a very small API surface.

Underlock

Underlock makes it dead simple to encrypt and decrypt your data and files. It comes with little to no dependencies and has a very small API surface.

Gem Version Code Climate Build Status

Installation

Add this line to your application's Gemfile:

gem 'underlock'

And then execute:

$ bundle

Or install it yourself as:

$ gem install underlock

Initialization

Underlock::Base.configure do |config|
  config.public_key  = File.read('./key.pub')
  config.private_key = File.read('./key.priv')
  config.cipher      = OpenSSL::Cipher.new('aes-256-gcm')
end

For the config.cipher value, all algorithms available in OpenSSL::Cipher.ciphers are supported.

Important Note: Choose your algorithm carefully and stick to it. It'll kind of suck to be not able to decrypt your encrypted data.

Generating Public/Private keypair

key = OpenSSL::PKey::RSA.new 4096
puts key.to_pem
puts key.public_key.to_pem

Usage

Encrypting Strings/Text

irb> Underlock::Base.encrypt("super secret message")
=> #<Underlock::EncryptedEntity:0x007fef2e4b8320>

Underlock::EncryptedEntity has the following 3 methods

encrypted_entity.value
encrypted_entity.key
encrypted_entity.iv # iv stands for initialization vector

You should persist or store the key and iv in order to be able to decrypt the encrypted value.

Decrypting Strings/Text

  • Create an instance of Underlock::EncryptedEntity, use the key and iv collected in the previous steps.
irb> encrypted_entity = Underlock::EncryptedEntity.new(value: value, key: key, iv: iv)
  • Decrypt using one of the following methods:
irb> encrypted_entity.decrypt
irb> Underlock::Base.decrypt(encrypted_entity)

Encrypting Files

To encrypt files, instead of passing a String object, pass a File object to Underlock::Base.encrypt

irb> file = File.open('/path/to/your/secret/file.txt')
irb> Underlock::Base.encrypt(file)
=> #<Underlock::EncryptedEntity:0x007fef2e4b8320>

The return value is an instance of Underlock::EncryptedEntity and has the following methods:

encrypted_entity.encrypted_file
encrypted_entity.key
encrypted_entity.iv # iv stands for initialization vector here

#encrypted_file returns a File object. This file is saved in the same directory as your original file.

Decrypting Files

  • Create an instance of Underlock::EncryptedEntity, use the key and iv collected in the previous steps.
irb> file = File.open('/path/to/your/secret/file.txt.enc')
irb> encrypted_entity = Underlock::EncryptedEntity.new(encrypted_file: file, key: key, iv: iv)
  • Decrypt using one of the following methods:
irb> encrypted_entity.decrypt
irb> Underlock::Base.decrypt(encrypted_entity)

Following naming scheme is followed when encrypting/decrypting files:

original file name encrypted file name decrypted file name
file.pdf file.pdf.enc file.decrypted.pdf

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/metaware/underlock.

License

The gem is available as open source under the terms of the MIT License.

More Repositories

1

angular-invoicing

Create free and unlimited one off invoices using this little Invoicing app made with Angular JS
HTML
662
star
2

flipper

Feature Flipper, Feature Flags, Rollout Flags, Feature Toggles for Crystal
Crystal
21
star
3

to_range

Small Ruby Gem to Convert Parseable Strings into Ruby Range Objects
Ruby
10
star
4

responsive_bootstrap_admin_theme

Responsive Bootsrap Admin Theme
CSS
8
star
5

hackernews_api

Turns Hacker News frontpage into a clean JSON API - Built using Elixir & Phoenix
Elixir
5
star
6

typescript-shopping-cart

Example Shopping Cart classes/interfaces to demonstrate usage of TypeScript in a NodeJS environment
JavaScript
4
star
7

age-calculator

Age Calculator we always wished we had.
JavaScript
3
star
8

manzoori

เจฎเฉฐเจœเฉ‚เจฐเฉ€ (manzoori): Add an approval process on top of your models
Ruby
2
star
9

graphql-rails-example

A sample showing GraphQL implementation in Ruby on Rails Framework
Ruby
2
star
10

react-invoicing

React + Redux Sample Invoicing App / Also uses reselect to compute derived state
JavaScript
2
star
11

elixir-nest

Generate nested namespaced keys for key-value databases with Elixir
Elixir
1
star
12

safetypay

Safetypay Ruby SDK
Ruby
1
star
13

waveapps-ruby

Ruby
1
star
14

elixir-fizzbuzz

Functional Solution to FizzBuzz problem (in Elixir)
Elixir
1
star
15

rainforest

Rainforest CLI written in NodeJS
JavaScript
1
star
16

tomtomplugin

Cordova plugin to plan a trip on TomTom
Java
1
star
17

elixir_processes

An example implementation of two ping-pong processes that are capable to chatter amongst themselves
Elixir
1
star
18

elixir-etudes

practice examples and solutions for http://chimera.labs.oreilly.com/books/1234000001642/
Elixir
1
star