reversible_cryptography 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b62a2069be44c9cf86f98fd026a43cd2a3d8e6be
|
4
|
+
data.tar.gz: 104d949e5d362e3808d8262be73bcf0fe52d76a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0af90075535d26a280902c8c0ffb25b2d433abf5c1cf64b17c221a92bf489ae81dfc3f0652032f9ccbc3877dc5269afa5f4abe19837f78bcbeeb3d6700626581
|
7
|
+
data.tar.gz: dff254aab06ef0ce53238e34c90c9c8860e56c46d3ed5f266a4366b4cf9e7fd5134bd7d1116a1fa284687890d7508f2f686325bb1042ec90f6bb55b42d66a300
|
@@ -2,5 +2,9 @@ require "reversible_cryptography/version"
|
|
2
2
|
require "reversible_cryptography/message"
|
3
3
|
|
4
4
|
module ReversibleCryptography
|
5
|
-
|
5
|
+
class BaseError < StandardError; end
|
6
|
+
class EmptyInputString < BaseError; end
|
7
|
+
class InvalidPassword < BaseError; end
|
8
|
+
class EmptyPassword < InvalidPassword; end
|
9
|
+
class InvalidFormat < BaseError; end
|
6
10
|
end
|
@@ -8,6 +8,9 @@ module ReversibleCryptography
|
|
8
8
|
|
9
9
|
class << self
|
10
10
|
def encrypt(str, password)
|
11
|
+
raise EmptyInputString if blank?(str)
|
12
|
+
raise EmptyPassword if blank?(password)
|
13
|
+
|
11
14
|
enc = OpenSSL::Cipher::AES256.new(MODE)
|
12
15
|
enc.encrypt
|
13
16
|
|
@@ -23,10 +26,17 @@ module ReversibleCryptography
|
|
23
26
|
end
|
24
27
|
|
25
28
|
def decrypt(str, password)
|
29
|
+
raise EmptyInputString if blank?(str)
|
30
|
+
raise EmptyPassword if blank?(password)
|
31
|
+
|
26
32
|
key = str.sub(/^md5:(.+):salt:(.+):#{PREFIX}:/, '')
|
27
33
|
md5 = $1
|
28
34
|
salt_string = $2
|
29
35
|
|
36
|
+
if [key, md5, salt_string].any? { |s| blank?(s) }
|
37
|
+
raise InvalidFormat
|
38
|
+
end
|
39
|
+
|
30
40
|
salt = convert_salt(salt_string)
|
31
41
|
|
32
42
|
dec = OpenSSL::Cipher::AES256.new(MODE)
|
@@ -37,7 +47,7 @@ module ReversibleCryptography
|
|
37
47
|
if OpenSSL::Digest.hexdigest("MD5", result) == md5
|
38
48
|
result
|
39
49
|
else
|
40
|
-
raise
|
50
|
+
raise InvalidPassword
|
41
51
|
end
|
42
52
|
end
|
43
53
|
|
@@ -56,6 +66,10 @@ module ReversibleCryptography
|
|
56
66
|
def generate_salt_string(salt)
|
57
67
|
salt.unpack("C*").join("-")
|
58
68
|
end
|
69
|
+
|
70
|
+
def blank?(str)
|
71
|
+
str.nil? || str.empty?
|
72
|
+
end
|
59
73
|
end
|
60
74
|
end
|
61
75
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reversible_cryptography
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Takumi MIURA
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03
|
11
|
+
date: 2015-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|