crypto-lite 0.0.1 → 0.1.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
  SHA256:
3
- metadata.gz: 6408a018c374d22c0aabb5c532ec5ad7a32c6498d9d387d5e5042849c9d82dc5
4
- data.tar.gz: 30c6920e588bcbc908f92ae72d506c74c668079570222d60e7b7d8a306e3846e
3
+ metadata.gz: d7c97262d69928c8f9b51816fcf3fff1f5bc61ba5bf49bc81d011ec9567be877
4
+ data.tar.gz: bfb41f345684cc2f27256e64d95f4a4535b908784f6353643bfef251c83262b7
5
5
  SHA512:
6
- metadata.gz: 8c81352f2acfa641de7b6eaebbf6b0c520f0439db3be966245dd713ede27bcc2da97746e6966e9412fc2a05bc552948189fe3fa5bb02c71b0a822347c6211cda
7
- data.tar.gz: 2b6779c4a6234d21256ad260f9949e4951c5b940f91e25a749bcffd0fed9640e9c43bfd06e578f03ba254ee290d5cf08db9d3e58ace927312ae53aafca2629b3
6
+ metadata.gz: 450adb10806e9944fafc4fdf04bb3b09a59fa6fc20ac62a8c9b48b780711ef23555174765b7f545ded98607185bad5488f116a691b34d253de7b7cd30d84fd62
7
+ data.tar.gz: 524b2c9ae838156c20eb313a045057974e51339e912c48f6945864429ecfd958a7cc8236a83fbbd0121ebc119c2b4a0e76b4b55383b9e64e9ccee35363e912ea
data/README.md CHANGED
@@ -2,9 +2,186 @@
2
2
 
3
3
 
4
4
 
5
+ * home :: [github.com/rubycoco/blockchain](https://github.com/rubycoco/blockchain)
6
+ * bugs :: [github.com/rubycoco/blockchain/issues](https://github.com/rubycoco/blockchain/issues)
7
+ * gem :: [rubygems.org/gems/crypto-lite](https://rubygems.org/gems/crypto-lite)
8
+ * rdoc :: [rubydoc.info/gems/crypto-lite](http://rubydoc.info/gems/crypto-lite)
5
9
 
6
10
 
11
+ ## Usage
7
12
 
13
+ ### Secure Hashing / Hash Functions
8
14
 
15
+ SHA256 - Secure Hash Algorithm (SHA) 256-bits (32 bytes)
9
16
 
10
17
 
18
+ ``` ruby
19
+ require 'crypto' ## or use require 'crypto-lite'
20
+
21
+ ## try abc
22
+ sha256( "abc" ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
23
+ sha256( "abc".b ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
24
+ sha256( "\x61\x62\x63" ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
25
+ sha256( 0x616263 ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
26
+
27
+ pp sha256hex( '616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
28
+ pp sha256hex( '0x616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
29
+ pp sha256hex( '0X616263' ) #=> "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
30
+
31
+
32
+ ## try a
33
+ sha256( "a" ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
34
+ sha256( "\x61" ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
35
+ sha256( 0b01100001 ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
36
+ sha256( 0x61 ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
37
+
38
+ sha256hex( '61' ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
39
+ sha256hex( '0x61' ) #=> "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
40
+
41
+
42
+ ## try some more
43
+ sha256( "Hello, Cryptos!" ) #=> "33eedea60b0662c66c289ceba71863a864cf84b00e10002ca1069bf58f9362d5"
44
+ ```
45
+
46
+
47
+
48
+
49
+ ### Public Key Signature Algorithms
50
+
51
+ RSA - Rivest-Shamir-Adleman
52
+
53
+
54
+
55
+ ``` ruby
56
+ alice_key, alice_pub = RSA.generate_keys
57
+
58
+ alice_key
59
+ #=> "-----BEGIN RSA PRIVATE KEY-----
60
+ # MIIEpAIBAAKCAQEAzLpmAQ+MbUTHU1XxzEaQXqiOvk0Vu/skztaMWz+UoGYWU6eW
61
+ # cr7zVt/Y0SYqzD8LkYireX22FxNNFfhgu3/uC5yTl+dri6PD6NDAmrG+1cyE8kZZ
62
+ # MGq91wQEemZPuesjTgKEvwZbknjodIKOAP35QycMr4PuWICSrCjhJLrClI7jInTZ
63
+ # LOLtD5w5U7/xLOJAIfuhjUA4wrFCLJGPe7214KWgDCLmsan4/GVUloUKa6KAHJiH
64
+ # q4tNxNdSrbOlluZbKQl8REhXOCIb5bEX2KnbQT0nPgKkuOlXgZ7jeyOIk0FG1RGa
65
+ # FvcGu8LieMgT39WltcHJLblNkDr9YDRGiNiThQIDAQABAoIBAQCE/FPEPqBeXj4I
66
+ # MRzHL9MZ2e4XSaVjnYjUXuN/ZnaaFpZMMuF0mfshpHiHq35DfHR8TcXtPi6pIJ2D
67
+ # NvtG8JvlqQjqtKXUaEWbFvb1xZ4L7TUy12WaIMw+PlrWU11YjJg7VUF7gJq9M5L0
68
+ # E9ZAaLmg2F3SKSYLEUG1WTyeij5ZFqouNjZxD2xo5U5Agy2UVm2D9aUm/n4g8Wnr
69
+ # HybadhD6V9+BsZ2e9Q6CamHRah9Hs4nDPnycPFXpbs32wx9nvACPMg5+/Fqxr/ZK
70
+ # cPM4syVBW0lNhpTzhHkPvimAgwgqJYvAj/o9nQnq5i1XyVyXp3uKVnld3FCddf9i
71
+ # ovQMPmVlAoGBAPHtUKRehy8df/Zw6oGz0WcZCTjEwZ9DEb5rFN9Pr2IyvOhmZ3UJ
72
+ # JNx9WmiiGB44dbnafMtr2Ya7u4OAM6e190BbcJKTnpWqVlsXw/wyQqIgJb3AtFu4
73
+ # 91mqsDepOWsfs1IjTgmR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esjTgKEvwZbknjodIKOAP35
92
+ # QycMr4PuWICSrCjhJLrClI7jInTZLOLtD5w5U7/xLOJAIfuhjUA4wrFCLJGPe721
93
+ # 4KWgDCLmsan4/GVUloUKa6KAHJiHq4tNxNdSrbOlluZbKQl8REhXOCIb5bEX2Knb
94
+ # QT0nPgKkuOlXgZ7jeyOIk0FG1RGaFvcGu8LieMgT39WltcHJLblNkDr9YDRGiNiT
95
+ # hQIDAQAB
96
+ # -----END PUBLIC KEY-----"
97
+
98
+ bob_key, bob_pub = RSA.generate_keys
99
+
100
+ bob_key
101
+ #=> "-----BEGIN RSA PRIVATE KEY-----
102
+ # MIIEpAIBAAKCAQEAzADannvKlfVkZmKA4EDIxTW0HiJzjD6Auh8wLi02+iz2BScz
103
+ # fECA65Zv+KHfc1B9AWMqGeBIwFE49NrsnXiZwZR3DqcFS8WbnVqpntvhwzlEARna
104
+ # RWmZ2XjloD7fxILbXtWfMFNjwSfaK0bpArLkrt9d8eni+JI42+ptIWs/bVynACqm
105
+ # DqOTjoEgajuHVpxHtskPNQrsjxzP+umsUWkbE0iaO7oN1pcgZIR4VRr0bz/3Juif
106
+ # WmiCgwbDZo1WolfveoCacVsfAB1iesxeWnrGIJUjq8Mqsu9mQz1dg6RF4ElwNJ57
107
+ # G3T3nlW+qpVBZDU2sHFqUFxbGmWPdRUn1yn4KwIDAQABAoIBAQCOCwotz4P/Zh3C
108
+ # LFQP0Qv6RKplURejTuHStmSVwmXFTAkBDYqLuV4Kq3TLaepsIF7p2GI4IjKFtggy
109
+ # dTzLaG2mm/lJ+oF1gOIZbkcslW1cwULYgWe5bQ3ynntEWIL2ESctoRB2VZnfpCAE
110
+ # ghs8BdO071I6Xt/qs+VjOpdB7ar8OYhFc1vhwiI03FKbjuScH0CQOETIeLCqK5tC
111
+ # qPnjMTYdaTp/NgcZujsOeOBgbARLzGtCaESbmXHO6mPDkEED5uqZzsNBtdCZIGMF
112
+ # ApJkZbF6xSRizQhwwRlak1jCkAk2VCYpKPMiop1+cbjs3jU3RyP94RHc/yKo2Rzm
113
+ # HCl35XYBAoGBAPJDMV9W2scRsMlLw9In3ZzWtammcouE0oXEgizK61Cg/5C5E06a
114
+ # 5anrfwF5bURBANKBqTSHV0u71C2fHs1KO+B+EHzQ4DKsXldCSv2PR/0A6lmF9AIL
115
+ # DFfup/mU55plbqCnjJe2BOUrOmurSd5MbWtShRdGri/LBqF58BFgT+U1AoGBANeS
116
+ # RZDsCWelZPGN8Wxp9zxhu1AClNO9S7ITjZOQTYlghCVKAkS1wvB/6TIjaw8DyREs
117
+ # f6WvtkzQA/vZc4mXE+YM/calL8ee3wVEJJzlGBfuh8mQhxtiLa5PTl7Icv/R8DGV
118
+ # 9hU9GkJgWdi/+Plpqdcv79OWVMTB7igmoN8PAPPfAoGAKqatwI04AygYKbhPB2bB
119
+ # W2Vpoi6NqAaAUdCg4mXvO8i8daw/u+0FVf8B4y6PkB6pmGX/diIFum2dE1MaRyY0
120
+ # mHdZS8AyWHmEOnSPY0igceiBWbV9mgZ769c2d3hBtir5aQtWczc2cWpE5MPJQ3vN
121
+ # H8HtcIWfEQb7ad5f548/QakCgYEAwFDjNRYOkePQ+Vrbjg+/HKRH+mpDId9Xv4eI
122
+ # H6R2N9/eJHIxMeFCB1Ll1PAaG6wR3ftn6YWnykEtvKpTU+VvQCZI5MYLqTgH2Ofh
123
+ # DgOoCfmoNF922SwuerqPvSlwxt8hPOt/PZVkbuEMZr1lPgVRGwPOHmKYP2yPrkw/
124
+ # 6p+1BtsCgYABmMLgWhXVD19XxNHm8XpGnPWTEjqAYrw6I5yDUwNhB0n4129qaC+x
125
+ # MWrdslKBmQh1r1U5QoSSL0CY4Ef5qN02uZl15FN1kYQzZA6kJi+MoBsjzrZCvzsc
126
+ # Bbahpg363PyHC75zgvazvOr4tK3mzaRi5RNTMgivTVu4FyhkRdJ5wQ==
127
+ # -----END RSA PRIVATE KEY-----"
128
+
129
+ bob_pub
130
+ #=> "-----BEGIN PUBLIC KEY-----
131
+ # MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzADannvKlfVkZmKA4EDI
132
+ # xTW0HiJzjD6Auh8wLi02+iz2BSczfECA65Zv+KHfc1B9AWMqGeBIwFE49NrsnXiZ
133
+ # wZR3DqcFS8WbnVqpntvhwzlEARnaRWmZ2XjloD7fxILbXtWfMFNjwSfaK0bpArLk
134
+ # rt9d8eni+JI42+ptIWs/bVynACqmDqOTjoEgajuHVpxHtskPNQrsjxzP+umsUWkb
135
+ # E0iaO7oN1pcgZIR4VRr0bz/3JuifWmiCgwbDZo1WolfveoCacVsfAB1iesxeWnrG
136
+ # IJUjq8Mqsu9mQz1dg6RF4ElwNJ57G3T3nlW+qpVBZDU2sHFqUFxbGmWPdRUn1yn4
137
+ # KwIDAQAB
138
+ # -----END PUBLIC KEY-----"
139
+
140
+
141
+ tx = "from: alice, to: bob, $21"
142
+ tx_hash = sha256( tx )
143
+ #=> "426a472a6c69bf68354391b7822393bea3952cde9df8949ad7a0f5f405b2fcb5"
144
+
145
+ tx_signature = RSA.sign( tx_hash, alice_key )
146
+ #=> "xfhzC6tzXYmA5rFAFybJ9KeWnTcTnC0Plt7cSHky6ZSdBZRKz/sfFcpyIN7w
147
+ # jWrdPwEREA3nwNu/HSpiGRBFr+lu/YgWGNp6HLGPeL7uHGAfmWPyU5WRzGzf
148
+ # iEs5B6kdJ3S8LSbP0hkOD8AOgZLPeU5rzA4+/Ymt8e/UOVwwka6Gj13yoBua
149
+ # mSdsVuQfgh2VpySejCz4ykYlMSHK8Kx8QFt+QbyI5QZUy2dFh6HlcnHR+G9A
150
+ # RMRZ1vAuQhYqtDSsxwRcZCSFsc6uctAvsgFinhqy6ls5VpcXfuKwZhKAw3Di
151
+ # E2MYUnT7+i38Mq26iWzgmDbpOrVCO5tjlSiHY1731A=="
152
+
153
+ RSA.valid_signature?( tx_hash, tx_signature, alice_pub )
154
+ #=> true
155
+
156
+ tx = "from: alice, to: bob, $22"
157
+ tx_hash = sha256( tx )
158
+ #=> "e899604bb4c95d2f1a7cfe561ad65941769e2064bdbbcaa79eb64ce0a2832380"
159
+
160
+ RSA.valid_signature?( tx_hash, tx_signature, alice_pub )
161
+ #=> false
162
+ ```
163
+
164
+
165
+ and some more.
166
+
167
+
168
+
169
+
170
+ ## Install
171
+
172
+ Just install the gem:
173
+
174
+ $ gem install crypto-lite
175
+
176
+
177
+ ## License
178
+
179
+ The scripts are dedicated to the public domain.
180
+ Use it as you please with no restrictions whatsoever.
181
+
182
+
183
+ ## Questions? Comments?
184
+
185
+ Send them along to the [wwwmake forum](http://groups.google.com/group/wwwmake).
186
+ Thanks!
187
+
data/Rakefile CHANGED
@@ -1,11 +1,12 @@
1
1
  require 'hoe'
2
2
  require './lib/crypto-lite/version.rb'
3
3
 
4
+
4
5
  Hoe.spec 'crypto-lite' do
5
6
 
6
7
  self.version = CryptoLite::VERSION
7
8
 
8
- self.summary = "crypto-lite - Cryptographic Secure Hash Functions and Public Key Signature Algorithms Made Easy"
9
+ self.summary = "crypto-lite - cryptographic secure hash functions and public key signature algorithms made easy"
9
10
  self.description = summary
10
11
 
11
12
  self.urls = { home: 'https://github.com/rubycoco/blockchain' }
@@ -1,5 +1,6 @@
1
1
  require 'pp'
2
2
  require 'digest'
3
+ require 'base64'
3
4
  require 'openssl'
4
5
 
5
6
 
@@ -68,14 +69,46 @@ module Crypto
68
69
  count == 1 ? "#{count} #{noun}" : "#{count} #{noun}s"
69
70
  end
70
71
 
72
+
73
+
74
+ module RSA
75
+ def self.generate_keys ## todo/check: add a generate alias - why? why not?
76
+ key_pair = OpenSSL::PKey::RSA.new( 2048 )
77
+ private_key = key_pair.export
78
+ public_key = key_pair.public_key.export
79
+
80
+ [private_key, public_key]
81
+ end
82
+
83
+
84
+ def self.sign( plaintext, private_key )
85
+ private_key = OpenSSL::PKey::RSA.new( private_key ) ## note: convert/wrap into to obj from exported text format
86
+ Base64.encode64( private_key.private_encrypt( plaintext ))
87
+ end
88
+
89
+ def self.decrypt( ciphertext, public_key )
90
+ public_key = OpenSSL::PKey::RSA.new( public_key ) ## note: convert/wrap into to obj from exported text format
91
+ public_key.public_decrypt( Base64.decode64( ciphertext ))
92
+ end
93
+
94
+
95
+ def self.valid_signature?( plaintext, ciphertext, public_key )
96
+ plaintext == decrypt( ciphertext, public_key )
97
+ end
98
+ end # module RSA
71
99
  end # module Crypto
72
100
 
73
101
 
74
102
 
103
+
75
104
  ## add convenience "top-level" helpers
76
105
  def sha256( input, engine=nil ) Crypto.sha256( input, engine ); end
77
106
  def sha256hex( input, engine=nil ) Crypto.sha256hex( input, engine ); end
78
107
 
108
+ RSA = Crypto::RSA
109
+
110
+
111
+
79
112
 
80
113
 
81
114
 
@@ -2,8 +2,8 @@
2
2
  module CryptoLite
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 0
6
- PATCH = 1
5
+ MINOR = 1
6
+ PATCH = 0
7
7
  VERSION = [MAJOR,MINOR,PATCH].join('.')
8
8
 
9
9
  def self.version
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crypto-lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
@@ -44,8 +44,8 @@ dependencies:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
46
  version: '3.22'
47
- description: crypto-lite - Cryptographic Secure Hash Functions and Public Key Signature
48
- Algorithms Made Easy
47
+ description: crypto-lite - cryptographic secure hash functions and public key signature
48
+ algorithms made easy
49
49
  email: wwwmake@googlegroups.com
50
50
  executables: []
51
51
  extensions: []
@@ -89,6 +89,6 @@ requirements: []
89
89
  rubygems_version: 3.1.4
90
90
  signing_key:
91
91
  specification_version: 4
92
- summary: crypto-lite - Cryptographic Secure Hash Functions and Public Key Signature
93
- Algorithms Made Easy
92
+ summary: crypto-lite - cryptographic secure hash functions and public key signature
93
+ algorithms made easy
94
94
  test_files: []