cryptify 0.0.1 → 0.0.2
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 +4 -4
- data/Rakefile +2 -9
- data/cryptify.gemspec +4 -1
- data/lib/cryptify.rb +117 -3
- data/lib/cryptify/version.rb +1 -1
- data/spec/cryptify_spec.rb +40 -0
- data/spec/spec_helper.rb +1 -0
- data/tasks/rspec.rake +3 -0
- metadata +29 -8
- data/test/cryptify_test.rb +0 -9
- data/test/test_helper.rb +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d199af44df145429bd4b0fc5b35f54c64f9c25a
|
4
|
+
data.tar.gz: e7157ef88b167976c2941f65d9799ad433cdd086
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84dd0654015cae58eb810ddd02f38628158260bd6cd6dbf03090822aac08b5a82a5cf2fb4521ef72a5e3045893fa5194ab51ee1fca927561ec81bf811c6d5455
|
7
|
+
data.tar.gz: c5764003c196130abf920c1fb8311128be61cd07713fa83bbf952f526ac02707cede394d5767b394dd19b938622eeef5ecd91ebbe89288a70fcfc90158b7243e
|
data/Rakefile
CHANGED
@@ -1,12 +1,5 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
require "rake/testtask"
|
3
3
|
|
4
|
-
#
|
5
|
-
|
6
|
-
t.libs << "test"
|
7
|
-
t.test_files = FileList["test/**/*_test.rb"]
|
8
|
-
t.verbose = true
|
9
|
-
end
|
10
|
-
|
11
|
-
# Default task.
|
12
|
-
# task default: :test
|
4
|
+
# RSpect tasks.
|
5
|
+
Dir.glob("tasks/**/*.rake").each(&method(:import))
|
data/cryptify.gemspec
CHANGED
@@ -9,7 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Pablo Vallejo"]
|
10
10
|
spec.email = ["pabloninety@gmail.com"]
|
11
11
|
spec.summary = %q{RSA implementation.}
|
12
|
-
spec.description = %q{Simple RSA encoder and decoder
|
12
|
+
spec.description = %q{Simple RSA encoder and decoder implementation.
|
13
|
+
NOTE: this gem is not intended to be used in production.}
|
13
14
|
spec.homepage = ""
|
14
15
|
spec.license = "MIT"
|
15
16
|
|
@@ -20,4 +21,6 @@ Gem::Specification.new do |spec|
|
|
20
21
|
|
21
22
|
spec.add_development_dependency "bundler", "~> 1.7"
|
22
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
# spec.add_development_dependency "rspec", ">= 3.1.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.1", ">= 3.1.0"
|
23
26
|
end
|
data/lib/cryptify.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "cryptify/version"
|
2
|
+
require 'prime'
|
2
3
|
|
3
4
|
module Cryptify
|
4
5
|
|
@@ -6,10 +7,123 @@ module Cryptify
|
|
6
7
|
# RSA implementation class.
|
7
8
|
#
|
8
9
|
class Rsa
|
9
|
-
|
10
|
-
|
10
|
+
|
11
|
+
#
|
12
|
+
# Attributes used.
|
13
|
+
#
|
14
|
+
# @p, @q
|
15
|
+
# Random prime numbers.
|
16
|
+
#
|
17
|
+
# @e
|
18
|
+
# Public exponent
|
19
|
+
#
|
20
|
+
# @n
|
21
|
+
# @p and @q computation
|
22
|
+
#
|
23
|
+
# @d
|
24
|
+
# Inverse multiplicative
|
25
|
+
#
|
26
|
+
|
27
|
+
|
28
|
+
#
|
29
|
+
# Construtor.
|
30
|
+
#
|
31
|
+
def initialize
|
32
|
+
|
33
|
+
prime_list = Prime.take(100)
|
34
|
+
|
35
|
+
# Generate p and q
|
36
|
+
@p = prime_list.sample
|
37
|
+
prime_list.delete(@p)
|
38
|
+
@q = prime_list.sample
|
39
|
+
|
40
|
+
# Compute n.
|
41
|
+
@n = @p * @q
|
42
|
+
|
43
|
+
# 1.3 Calculate `z`.
|
44
|
+
@z = (@p - 1) * (@q - 1)
|
45
|
+
|
46
|
+
# 1.4 Calculate coprime.
|
47
|
+
@z.downto(0) do |i|
|
48
|
+
if self.is_coprime(i, @z)
|
49
|
+
@e = i; break;
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# 1.5 Modular inverse
|
54
|
+
@d = modinv(@e, @z)
|
55
|
+
|
11
56
|
end
|
12
|
-
end
|
13
57
|
|
58
|
+
|
59
|
+
#
|
60
|
+
# Encrypt a message.
|
61
|
+
#
|
62
|
+
def encrypt(message)
|
63
|
+
|
64
|
+
# Represent the message as a positive integer.
|
65
|
+
# message = 29
|
66
|
+
|
67
|
+
# Compute the ciphertext c to b.
|
68
|
+
(message ** @e) % @n
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
#
|
73
|
+
# Decrypt message.
|
74
|
+
#
|
75
|
+
def decrypt(encrypted_message)
|
76
|
+
|
77
|
+
# Uses his private key (n, d) to compute:
|
78
|
+
(encrypted_message ** @d) % @n
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
#
|
84
|
+
# Checks whether `a` and `b` are coprime.
|
85
|
+
#
|
86
|
+
def is_coprime(a, b)
|
87
|
+
|
88
|
+
# Cycle from 2 until the smaller number of a and b.
|
89
|
+
2.upto([a, b].min) do |i|
|
90
|
+
|
91
|
+
# If both modulus with `i` are equal to `0`
|
92
|
+
# then, the numbers are no coprime.
|
93
|
+
if a % i == b % i && a % i == 0
|
94
|
+
return false
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
return true
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
#
|
103
|
+
# Returns a triple (g, x, y), such that ax + by = g = gcd(a, b).
|
104
|
+
# Assumes a, b >= 0, and that at least one of them is > 0.
|
105
|
+
# Bounds on output values: |x|, |y| < [a, b].max
|
106
|
+
#
|
107
|
+
def egcd(a, b)
|
108
|
+
|
109
|
+
if a == 0
|
110
|
+
return [b, 0, 1]
|
111
|
+
else
|
112
|
+
g, y, x = egcd(b % a, a)
|
113
|
+
return [g, x - (b / a) * y, y]
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
#
|
119
|
+
# Modular inverse.
|
120
|
+
#
|
121
|
+
def modinv(a, m)
|
122
|
+
g, x, y = self.egcd(a, m)
|
123
|
+
return g != 1 ? nil : x % m
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
end
|
14
128
|
end
|
15
129
|
|
data/lib/cryptify/version.rb
CHANGED
@@ -0,0 +1,40 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
|
4
|
+
describe Cryptify do
|
5
|
+
subject { Cryptify.new }
|
6
|
+
|
7
|
+
describe '#Process' do
|
8
|
+
|
9
|
+
it 'Should pass' do
|
10
|
+
expect('debug').to eq 'debug'
|
11
|
+
|
12
|
+
# RSA tests.
|
13
|
+
RSA = Cryptify::Rsa.new
|
14
|
+
|
15
|
+
puts 'Encryption of text'
|
16
|
+
# puts KCS1.i2osp(encrypt_integer(PKCS1.os2ip(plaintext), options))
|
17
|
+
|
18
|
+
puts RSA.inspect
|
19
|
+
|
20
|
+
# puts 'Public keys'
|
21
|
+
# puts '-----------'
|
22
|
+
# puts RSA.get_public_keys
|
23
|
+
|
24
|
+
puts ''
|
25
|
+
puts 'Encryped message'
|
26
|
+
puts '-----------'
|
27
|
+
encrypted_message = RSA.encrypt(33)
|
28
|
+
puts encrypted_message
|
29
|
+
|
30
|
+
puts ''
|
31
|
+
puts 'Decrypted message'
|
32
|
+
puts '------------'
|
33
|
+
puts RSA.decrypt(encrypted_message)
|
34
|
+
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "cryptify"
|
data/tasks/rspec.rake
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cryptify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pablo Vallejo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,7 +38,29 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
-
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.1'
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 3.1.0
|
51
|
+
type: :development
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '3.1'
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 3.1.0
|
61
|
+
description: |-
|
62
|
+
Simple RSA encoder and decoder implementation.
|
63
|
+
NOTE: this gem is not intended to be used in production.
|
42
64
|
email:
|
43
65
|
- pabloninety@gmail.com
|
44
66
|
executables: []
|
@@ -53,8 +75,9 @@ files:
|
|
53
75
|
- cryptify.gemspec
|
54
76
|
- lib/cryptify.rb
|
55
77
|
- lib/cryptify/version.rb
|
56
|
-
-
|
57
|
-
-
|
78
|
+
- spec/cryptify_spec.rb
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
- tasks/rspec.rake
|
58
81
|
homepage: ''
|
59
82
|
licenses:
|
60
83
|
- MIT
|
@@ -79,6 +102,4 @@ rubygems_version: 2.2.2
|
|
79
102
|
signing_key:
|
80
103
|
specification_version: 4
|
81
104
|
summary: RSA implementation.
|
82
|
-
test_files:
|
83
|
-
- test/cryptify_test.rb
|
84
|
-
- test/test_helper.rb
|
105
|
+
test_files: []
|
data/test/cryptify_test.rb
DELETED
data/test/test_helper.rb
DELETED