crypto-lite 0.2.3 → 0.3.0
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/README.md +115 -105
- data/Rakefile +1 -0
- data/lib/crypto-lite.rb +1 -0
- data/lib/crypto-lite/version.rb +2 -2
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e32db0712ee1ed53c0720f9dee44533ffb8482e3d9ac6f5bd7a7e6a5c716cdc8
|
4
|
+
data.tar.gz: c3e8da15486a23e26d72c97c96bfc4f4c1847c420e7bff833c0ed87efe61303c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6593de7e55dbaa353cef41df3cf25cf04fd17cef78f9e2c62fbdcf0bf234627e4ec0dc2f238a341d5de8fa8b3ae33045cb8cf468c80f5ea8a55f312ff7cc0a0
|
7
|
+
data.tar.gz: 2e54d60bd80f34568ff7a6852fc3da01d3066131d4c177c023c1c15b4497fe18af7ad28851668b9c45223cab4f7eb8089759fc9e51fac714820b4a7d3a509ef7
|
data/README.md
CHANGED
@@ -38,6 +38,7 @@ Bonus Back Stage Tip: How does SHA256 work?
|
|
38
38
|
|
39
39
|
Try this [amazing animation of the SHA256 hash function in your very own terminal](https://github.com/in3rsha/sha256-animation) by Greg Walker.
|
40
40
|
|
41
|
+
More of a code golfer? See [½ Kilo of SHA256](https://idiosyncratic-ruby.com/51-half-kilo-of-sha256.html) by Jan Lelis - yes, the SHA256 algorithm coded (from scratch) in 500 bytes of ruby.
|
41
42
|
|
42
43
|
|
43
44
|
Onwards with more sha256 examples:
|
@@ -259,122 +260,131 @@ unbase58check( "1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs" ) #=> "00f54a5851e9372b878
|
|
259
260
|
|
260
261
|
### Public Key Signature Algorithms
|
261
262
|
|
262
|
-
**RSA - Rivest, Shamir and Adleman**
|
263
263
|
|
264
|
+
**Elliptic Curve Digital Signature Algorithm (ECDSA)**
|
265
|
+
|
266
|
+
|
267
|
+
Private Key
|
268
|
+
|
269
|
+
An ECDSA (Elliptic Curve Digital Signature Algorithm) private key is a random number between 1 and the order of the elliptic curve group.
|
264
270
|
|
265
271
|
|
266
272
|
``` ruby
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
#
|
286
|
-
#
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
#=> "
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
#
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
#
|
314
|
-
|
315
|
-
#
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
#
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
#
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
#
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
#
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
tx_hash = sha256( tx )
|
354
|
-
#=> "426a472a6c69bf68354391b7822393bea3952cde9df8949ad7a0f5f405b2fcb5"
|
355
|
-
|
356
|
-
tx_signature = RSA.sign( tx_hash, alice_key )
|
357
|
-
#=> "xfhzC6tzXYmA5rFAFybJ9KeWnTcTnC0Plt7cSHky6ZSdBZRKz/sfFcpyIN7w
|
358
|
-
# jWrdPwEREA3nwNu/HSpiGRBFr+lu/YgWGNp6HLGPeL7uHGAfmWPyU5WRzGzf
|
359
|
-
# iEs5B6kdJ3S8LSbP0hkOD8AOgZLPeU5rzA4+/Ymt8e/UOVwwka6Gj13yoBua
|
360
|
-
# mSdsVuQfgh2VpySejCz4ykYlMSHK8Kx8QFt+QbyI5QZUy2dFh6HlcnHR+G9A
|
361
|
-
# RMRZ1vAuQhYqtDSsxwRcZCSFsc6uctAvsgFinhqy6ls5VpcXfuKwZhKAw3Di
|
362
|
-
# E2MYUnT7+i38Mq26iWzgmDbpOrVCO5tjlSiHY1731A=="
|
363
|
-
|
364
|
-
RSA.valid_signature?( tx_hash, tx_signature, alice_pub )
|
273
|
+
# Auto-generate (random) private key
|
274
|
+
private_key = EC::PrivateKey.generate # by default uses Secp256k1 curve (used in Bitcoin and Ethereum)
|
275
|
+
|
276
|
+
private_key.to_i
|
277
|
+
#=> 72190737707147846840353520312904745954595478835413056312168022784020322830309
|
278
|
+
```
|
279
|
+
|
280
|
+
|
281
|
+
(Auto-)Calculate the Public Key - Enter Elliptic Curve (EC) Cryptography
|
282
|
+
|
283
|
+
The public key are two numbers (that is, a point with the coordinates x and y) computed by multiplying
|
284
|
+
the generator point (`G`) of the curve with the private key.
|
285
|
+
This is equivalent to adding the generator to itself `private_key` times.
|
286
|
+
Magic?
|
287
|
+
Let's try:
|
288
|
+
|
289
|
+
|
290
|
+
``` ruby
|
291
|
+
# This private key is just an example. It should be much more secure!
|
292
|
+
private_key = EC::PrivateKey.new( 1234 ) # by default uses Secp256k1 curve (used in Bitcoin and Ethereum)
|
293
|
+
|
294
|
+
public_key = private_key.public_key ## the "magic" one-way K=k*G curve multiplication (K=public key,k=private key, G=generator point)
|
295
|
+
point = public_key.point
|
296
|
+
|
297
|
+
point.x
|
298
|
+
#=> 102884003323827292915668239759940053105992008087520207150474896054185180420338
|
299
|
+
point.y
|
300
|
+
#=> 49384988101491619794462775601349526588349137780292274540231125201115197157452
|
301
|
+
|
302
|
+
point.x.to_s(16)
|
303
|
+
#=> "e37648435c60dcd181b3d41d50857ba5b5abebe279429aa76558f6653f1658f2"
|
304
|
+
point.y.to_s(16)
|
305
|
+
#=> "6d2ee9a82d4158f164ae653e9c6fa7f982ed8c94347fc05c2d068ff1d38b304c"
|
306
|
+
```
|
307
|
+
|
308
|
+
|
309
|
+
Sign a transaction with an (elliptic curve) private key:
|
310
|
+
|
311
|
+
``` ruby
|
312
|
+
# Step 1 - Calculate the Transaction (tx) Hash
|
313
|
+
tx = 'from: Alice to: Bob cryptos: 43_000_000_000'
|
314
|
+
txhash = sha256( tx )
|
315
|
+
|
316
|
+
# Step 2 - Get the Signer's Private key
|
317
|
+
private_key = EC::PrivateKey.new( 1234 ) # This private key is just an example. It should be much more secure!
|
318
|
+
|
319
|
+
# Sign!
|
320
|
+
signature = private_key.sign( txhash )
|
321
|
+
# -or-
|
322
|
+
signature = EC.sign( txhash, private_key )
|
323
|
+
|
324
|
+
signature.r
|
325
|
+
#=> 80563021554295584320113598933963644829902821722081604563031030942154621916407
|
326
|
+
signature.s
|
327
|
+
#=> 58316177618967642068351252425530175807242657664855230973164972803783751708604
|
328
|
+
|
329
|
+
signature.r.to_s(16)
|
330
|
+
#=> "3306a2f81ad2b2f62ebe0faec129545bc772babe1ca5e70f6e56556b406464c0"
|
331
|
+
signature.s.to_s(16)
|
332
|
+
#=> "4fe202bb0835758f514cd4a0787986f8f6bf303df629dc98c5b1a438a426f49a"
|
333
|
+
```
|
334
|
+
|
335
|
+
|
336
|
+
Verify a signed transaction with an (elliptic curve) public key:
|
337
|
+
|
338
|
+
``` ruby
|
339
|
+
# Step 1 - Calculate the Transaction (tx) Hash
|
340
|
+
tx = 'from: Alice to: Bob cryptos: 43_000_000_000'
|
341
|
+
txhash = sha256( tx )
|
342
|
+
|
343
|
+
# Step 2 - Get the Signer's Public Key
|
344
|
+
public_key = EC::PublicKey.new(
|
345
|
+
102884003323827292915668239759940053105992008087520207150474896054185180420338,
|
346
|
+
49384988101491619794462775601349526588349137780292274540231125201115197157452
|
347
|
+
)
|
348
|
+
|
349
|
+
# Step 3 - Get the Transaction's Signature
|
350
|
+
signature = EC::Signature.new(
|
351
|
+
80563021554295584320113598933963644829902821722081604563031030942154621916407,
|
352
|
+
58316177618967642068351252425530175807242657664855230973164972803783751708604
|
353
|
+
)
|
354
|
+
|
355
|
+
# Don't Trust - Verify
|
356
|
+
public_key.verify?( txhash, signature )
|
357
|
+
# -or-
|
358
|
+
EC.verify?( txhash, signature, public_key )
|
365
359
|
#=> true
|
366
360
|
|
367
|
-
tx = "from: alice, to: bob, $22"
|
368
|
-
tx_hash = sha256( tx )
|
369
|
-
#=> "e899604bb4c95d2f1a7cfe561ad65941769e2064bdbbcaa79eb64ce0a2832380"
|
370
361
|
|
371
|
-
|
372
|
-
|
362
|
+
# or using hexadecimal numbers
|
363
|
+
|
364
|
+
public_key = EC::PublicKey.new(
|
365
|
+
0xe37648435c60dcd181b3d41d50857ba5b5abebe279429aa76558f6653f1658f2,
|
366
|
+
0x6d2ee9a82d4158f164ae653e9c6fa7f982ed8c94347fc05c2d068ff1d38b304c
|
367
|
+
)
|
368
|
+
|
369
|
+
signature = EC::Signature.new(
|
370
|
+
0x3306a2f81ad2b2f62ebe0faec129545bc772babe1ca5e70f6e56556b406464c0,
|
371
|
+
0x4fe202bb0835758f514cd4a0787986f8f6bf303df629dc98c5b1a438a426f49a
|
372
|
+
)
|
373
|
+
|
374
|
+
public_key.verify?( txhash, signature )
|
375
|
+
# -or-
|
376
|
+
EC.verify?( txhash, signature, public_key )
|
377
|
+
#=> true
|
373
378
|
```
|
374
379
|
|
375
380
|
|
376
|
-
|
381
|
+
To sum up:
|
382
|
+
|
383
|
+
- The (raw) private key is a 256-bit unsigned integer number
|
384
|
+
- The (raw) public key is a point (x,y), that is, two 256-bit unsigned integer numbers - derived (calculated) from the private key
|
385
|
+
- A (raw) signature is composed of (r,s), that is, two 256-bit unsigned integer numbers
|
377
386
|
|
387
|
+
That's all the magic.
|
378
388
|
|
379
389
|
|
380
390
|
|
data/Rakefile
CHANGED
data/lib/crypto-lite.rb
CHANGED
data/lib/crypto-lite/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crypto-lite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-01-
|
11
|
+
date: 2021-01-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: digest-sha3-patched
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: elliptic
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rdoc
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|