lazy_rsa 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/lazy_rsa.rb +49 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 21e9a09c2b446c925d4ac119589f7f24b549f7bc662863dd5d64c4c41812e336
|
4
|
+
data.tar.gz: 9bdc9ceda55df07759aba32d77a8f92c0328c2edb29bdfe27f0a7f107083804c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 70a6c4abc45ea146e15727fd1fc3cd5842b83a779112530cb333f401e4de5b39f63e6fda9cf15d785476b5c6a0d122660bfb7a28cf6aa28c18d7a498ae0f5833
|
7
|
+
data.tar.gz: 59709a1f78ccdf10579cdb5849d03fb18c5cd898238286af6762352187493c01fce9c6d8b4c0f4409dbb5e7b18bba81e77509496eca0e5ceb1ee684d78478d48
|
data/lib/lazy_rsa.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
module LazyRsa
|
2
|
+
KeyParams = Data.define(:n, :e, :d)
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def encrypt(key, plaintext)
|
6
|
+
key = read_key(key)
|
7
|
+
text_to_integer(plaintext).mod_exp(key.e, key.n).to_i
|
8
|
+
end
|
9
|
+
|
10
|
+
def decrypt(key, ciphertext)
|
11
|
+
key = read_key(key)
|
12
|
+
OpenSSL::BN.new(ciphertext).mod_exp(key.d, key.n).to_i
|
13
|
+
end
|
14
|
+
|
15
|
+
def generate_key
|
16
|
+
q = OpenSSL::BN.generate_prime(512)
|
17
|
+
phi = OpenSSL::BN.new(0)
|
18
|
+
while q.gcd(phi) != 1
|
19
|
+
p = OpenSSL::BN.generate_prime(512)
|
20
|
+
phi = (p - 1) * (q - 1)
|
21
|
+
end
|
22
|
+
n = p * q
|
23
|
+
e = p.mod_exp(Constants::SECURE_EXPONENT, Constants::SECURE_MODULUS)
|
24
|
+
d = e.mod_inverse(phi)
|
25
|
+
|
26
|
+
build_key(e:, d:, n:)
|
27
|
+
end
|
28
|
+
|
29
|
+
def build_key(e:, d: nil, n:)
|
30
|
+
data_sequence = OpenSSL::ASN1::Sequence([
|
31
|
+
OpenSSL::ASN1::Integer(n),
|
32
|
+
OpenSSL::ASN1::Integer(e),
|
33
|
+
OpenSSL::ASN1::Integer(d),
|
34
|
+
])
|
35
|
+
asn1 = OpenSSL::ASN1::Sequence(data_sequence)
|
36
|
+
OpenSSL::PKey::RSA.new(asn1.to_der)
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def read_key(key)
|
42
|
+
KeyParams.new(**key.params)
|
43
|
+
end
|
44
|
+
|
45
|
+
def text_to_integer(text)
|
46
|
+
OpenSSL::BN.new(text.unpack("H*").first.to_i(16))
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lazy_rsa
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nick Quaranto
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-05-12 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: WARNING! PLEASE DON'T USE THIS GEM SINCE IT HAVE INTENTIONAL BACKDOOR
|
14
|
+
(FOR EDUCATIONAL PURPOSE)
|
15
|
+
email: nick.quaranto@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/lazy_rsa.rb
|
21
|
+
homepage: https://rubygems.org/gems/lazy_rsa
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubygems_version: 3.4.19
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Easy RSA
|
44
|
+
test_files: []
|