aes-everywhere 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 155bb40c51fe9156a54c72078e69de284da62629
4
+ data.tar.gz: cf7f4072971edf2d59b1e0a3e324e5b72f7623a7
5
+ SHA512:
6
+ metadata.gz: e996b2f5566d4d9752397c032bcc1283035afe069524960393a8309577c9eb1f0419dc1d18d75e8df54385797d69953ebd0b3afd391a0e46de3a6b10b888372a
7
+ data.tar.gz: 09909305669b288ba5d97006de16b170e8f2edad6183d508d19e7c31d5b9395b6abc8dfcb807af3c4e3c70e1626a91f0a542591ec51ae74a4bb8e74b04fe6400
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "openssl"
4
+ gem "base64"
5
+ gem 'digest', :git => 'https://github.com/hashview/digest'
@@ -0,0 +1,71 @@
1
+ # aes-everywhere.rb
2
+ # This file is part of AES-everywhere project (https://github.com/mervick/aes-everywhere)
3
+ #
4
+ # This is an implementation of the AES algorithm, specifically CBC mode,
5
+ # with 256 bits key length and PKCS7 padding.
6
+ #
7
+ # Copyright Andrey Izman (c) 2018-2019 <izmanw@gmail.com>
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ # of this software and associated documentation files (the "Software"), to deal
11
+ # in the Software without restriction, including without limitation the rights
12
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ # copies of the Software, and to permit persons to whom the Software is
14
+ # furnished to do so, subject to the following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be included in all
17
+ # copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ # SOFTWARE.
26
+
27
+
28
+ require "openssl"
29
+ require "digest"
30
+ require "base64"
31
+
32
+
33
+ class AES256
34
+ def self.encrypt(input, passphrase)
35
+ salt = OpenSSL::Random.random_bytes(8)
36
+ cipher = OpenSSL::Cipher::AES256.new(:CBC)
37
+ cipher.encrypt
38
+ cipher.key, cipher.iv = self.derive_key_and_iv(passphrase, salt)
39
+ crypted = cipher.update(input) + cipher.final
40
+ Base64.strict_encode64("Salted__" + salt + crypted)
41
+ end
42
+
43
+ def self.decrypt(crypted, passphrase)
44
+ data = Base64.strict_decode64(crypted)
45
+ salted = data[0..7]
46
+ if salted != "Salted__"
47
+ raise "Invalid data"
48
+ end
49
+ salt = data[8..15]
50
+ crypted = data[16..-1]
51
+ cipher = OpenSSL::Cipher::AES256.new(:CBC)
52
+ cipher.decrypt
53
+ cipher.key, cipher.iv = self.derive_key_and_iv(passphrase, salt)
54
+ derypted = cipher.update(crypted) + cipher.final
55
+ derypted.force_encoding("utf-8")
56
+ end
57
+
58
+ private
59
+
60
+ def self.derive_key_and_iv(passphrase, salt)
61
+ dx = di = ""
62
+ enc_pass = passphrase.bytes.pack('c*')
63
+
64
+ for _ in 1...4
65
+ di = Digest::MD5.digest(di + enc_pass + salt)
66
+ dx += di
67
+ end
68
+
69
+ return dx[0..31], dx[32..47]
70
+ end
71
+ end
@@ -0,0 +1,50 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require './lib/aes-everywhere.rb'
4
+
5
+
6
+ class AES256Test < Minitest::Test
7
+ def test_decrypt1
8
+ text = AES256.decrypt("U2FsdGVkX1+Z9xSlpZGuO2zo51XUtsCGZPs8bKQ/jYg=", "pass")
9
+ assert_equal text, "test"
10
+ end
11
+
12
+ def test_decrypt2
13
+ text = AES256.decrypt("U2FsdGVkX1+8b3WpGTbZHtd2T9PNQ+N7GqebGaOV3cI=", "Data πŸ˜„ тСкст")
14
+ assert_equal text, "test"
15
+ end
16
+
17
+ def test_decrypt3
18
+ text = AES256.decrypt("U2FsdGVkX18Kp+T3M9VajicIO9WGQQuAlMscLGiTnVyHRj2jHObWshzJXQ6RpJtW", "pass")
19
+ assert_equal text, "Data πŸ˜„ тСкст"
20
+ end
21
+
22
+ def test_decrypt4
23
+ text = AES256.decrypt("U2FsdGVkX1/O7iqht/fnrFdjn1RtYU7S+DD0dbQHB6N/k+CjzowfC2B21QRG24Gv", "Data πŸ˜„ тСкст")
24
+ assert_equal text, "Data πŸ˜„ тСкст"
25
+ end
26
+
27
+ def test_encrypt_decrypt1
28
+ text = "Test! @#$%^&*"
29
+ pass = "pass"
30
+ enc = AES256.encrypt(text, pass)
31
+ dec = AES256.decrypt(enc, pass)
32
+ assert_equal text, dec
33
+ end
34
+
35
+ def test_encrypt_decrypt2
36
+ text = "Test! @#$%^&*( πŸ˜†πŸ˜΅πŸ€‘πŸ‘Œ ε“ˆη½— こんにけわ ΠΠΊΡ—Ρž 😺"
37
+ pass = "pass"
38
+ enc = AES256.encrypt(text, pass)
39
+ dec = AES256.decrypt(enc, pass)
40
+ assert_equal text, dec
41
+ end
42
+
43
+ def test_encrypt_decrypt3
44
+ text = "Test! @#$%^&*( πŸ˜†πŸ˜΅πŸ€‘πŸ‘Œ ε“ˆη½— こんにけわ ΠΠΊΡ—Ρž 😺"
45
+ pass = "ε“ˆη½— こんにけわ ΠΠΊΡ—Ρž 😺"
46
+ enc = AES256.encrypt(text, pass)
47
+ dec = AES256.decrypt(enc, pass)
48
+ assert_equal text, dec
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aes-everywhere
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.5
5
+ platform: ruby
6
+ authors:
7
+ - Andrey Izman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: " AES Everywhere is Cross Language Encryption Library which provides
14
+ \n the ability to encrypt and decrypt data using a single algorithm in \n different
15
+ programming languages and on different platforms.\n This is an implementation
16
+ of the AES algorithm, specifically CBC mode, \n with 256 bits key length and
17
+ PKCS7 padding.\n"
18
+ email: izmanw@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - Gemfile
24
+ - lib/aes-everywhere.rb
25
+ - test/aes256_test.rb
26
+ homepage: https://github.com/mervick/aes-everywhere
27
+ licenses:
28
+ - MIT
29
+ metadata:
30
+ bug_tracker_uri: https://github.com/mervick/aes-everywhere/issues
31
+ source_code_uri: https://github.com/mervick/aes-everywhere/blob/master/ruby/lib/aes-everywhere.rb
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.6.6
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: AES Everywhere is Cross Language Encryption Library
52
+ test_files: []