encrypto_signo 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/encrypto_signo.rb +49 -0
- metadata +45 -0
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'base64'
|
3
|
+
|
4
|
+
module EncryptoSigno
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
# Returns an Base64 encoded string with encryption
|
9
|
+
def encrypt(key, string)
|
10
|
+
aes_encrypt = OpenSSL::Cipher::Cipher.new('AES-256-CBC').encrypt
|
11
|
+
aes_encrypt.key = aes_key = aes_encrypt.random_key
|
12
|
+
crypt = aes_encrypt.update(string) << aes_encrypt.final
|
13
|
+
encrypted_key = rsa_key(key).public_encrypt(aes_key)
|
14
|
+
[Base64.encode64(encrypted_key), Base64.encode64(crypt)].join("|")
|
15
|
+
end
|
16
|
+
|
17
|
+
## Return the raw string after decryption & decoding
|
18
|
+
def decrypt(key, string)
|
19
|
+
encrypted_key, crypt = string.split("|").map{|a| Base64.decode64(a) }
|
20
|
+
aes_key = rsa_key(key).private_decrypt(encrypted_key)
|
21
|
+
aes_decrypt = OpenSSL::Cipher::Cipher.new('AES-256-CBC').decrypt
|
22
|
+
aes_decrypt.key = aes_key
|
23
|
+
aes_decrypt.update(crypt) << aes_decrypt.final
|
24
|
+
end
|
25
|
+
|
26
|
+
# Return a signature for the string
|
27
|
+
def sign(key, string)
|
28
|
+
Base64.encode64(rsa_key(key).sign(OpenSSL::Digest::SHA1.new, string))
|
29
|
+
end
|
30
|
+
|
31
|
+
# Verify the string and signature
|
32
|
+
def verify(key, signature, string)
|
33
|
+
rsa_key(key).verify(OpenSSL::Digest::SHA1.new, Base64.decode64(signature), string)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Generate a keypair
|
37
|
+
def generate_keypair(size = 2048)
|
38
|
+
OpenSSL::PKey::RSA.new(size)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def rsa_key(key)
|
44
|
+
OpenSSL::PKey::RSA.new(key)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: encrypto_signo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adam Cooke
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-23 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email: adam@atechmedia.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/encrypto_signo.rb
|
21
|
+
homepage: http://atechmedia.com
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.23
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: Basic encryption and signing library for Ruby
|
45
|
+
test_files: []
|