aes-128 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/aes-128.rb +70 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7ec141ed7799aad3622fded0fde3436c167a743d
4
+ data.tar.gz: f7ae2cbcdee9925901053c81adfe5385a9b6002c
5
+ SHA512:
6
+ metadata.gz: d0bea477746934774f28fa0c8e465da845013e3a4a797c046a1b8eeef561bfbc071468365a473dbb0dbbdf67967afe5e923bfe0b9f5855fffeb015c1f252f519
7
+ data.tar.gz: b2c5650f4a449e85ae42565e838f3c12d217172a929111f08027b5977e32e95746cbefa962f1d3d096d520c8cd8449b6d3639a661b6435e735ce57d315f222a8
@@ -0,0 +1,70 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ # The encrypt_data and decrypt_data methods are Copyright (c) 2007 Brent Sowers
4
+ # and have been included with prior permission.
5
+ #
6
+ # I am creating this for my personal use and I did refer https://github.com/Gurpartap/aescrypt for licensing
7
+ #
8
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
12
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
13
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
14
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+
16
+ require 'openssl'
17
+ require 'base64'
18
+
19
+ module AES128
20
+ def self.encrypt(message, password)
21
+ Base64.encode64(self.encrypt_data(message.to_s.strip, self.key_digest(password), nil, "AES-128-CBC"))
22
+ end
23
+
24
+ def self.decrypt(message, password)
25
+ base64_decoded = Base64.decode64(message.to_s.strip)
26
+ self.decrypt_data(base64_decoded, self.key_digest(password), nil, "AES-128-CBC")
27
+ end
28
+
29
+ def self.key_digest(password)
30
+ OpenSSL::Digest::SHA256.new(password).digest
31
+ end
32
+
33
+ # Decrypts a block of data (encrypted_data) given an encryption key
34
+ # and an initialization vector (iv). Keys, iv's, and the data
35
+ # returned are all binary strings. Cipher_type should be
36
+ # "AES-128-CBC", "AES-128-ECB", or any of the cipher types
37
+ # supported by OpenSSL. Pass nil for the iv if the encryption type
38
+ # doesn't use iv's (like ECB).
39
+ #:return: => String
40
+ #:arg: encrypted_data => String
41
+ #:arg: key => String
42
+ #:arg: iv => String
43
+ #:arg: cipher_type => String
44
+ def self.decrypt_data(encrypted_data, key, iv, cipher_type)
45
+ aes = OpenSSL::Cipher::Cipher.new(cipher_type)
46
+ aes.decrypt
47
+ aes.key = key
48
+ aes.iv = iv if iv != nil
49
+ aes.update(encrypted_data) + aes.final
50
+ end
51
+
52
+ # Encrypts a block of data given an encryption key and an
53
+ # initialization vector (iv). Keys, iv's, and the data returned
54
+ # are all binary strings. Cipher_type should be "AES-128-CBC",
55
+ # "AES-128-ECB", or any of the cipher types supported by OpenSSL.
56
+ # Pass nil for the iv if the encryption type doesn't use iv's (like
57
+ # ECB).
58
+ #:return: => String
59
+ #:arg: data => String
60
+ #:arg: key => String
61
+ #:arg: iv => String
62
+ #:arg: cipher_type => String
63
+ def self.encrypt_data(data, key, iv, cipher_type)
64
+ aes = OpenSSL::Cipher::Cipher.new(cipher_type)
65
+ aes.encrypt
66
+ aes.key = key
67
+ aes.iv = iv if iv != nil
68
+ aes.update(data) + aes.final
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aes-128
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Gaurav Mamidwar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple AES-128 bit encryption / decryption for Ruby
14
+ email: gauravcm30@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/aes-128.rb
20
+ homepage: https://github.com/gauravcm30/aescrypt
21
+ licenses:
22
+ - ''
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.4.6
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: A simple and opinionated AES 128-bit encrypt / decrypt Ruby gem that just
44
+ works!
45
+ test_files: []