bytes 0.0.1 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bcdcc67a741e1c05bdd45da43251f2db46efe804
4
- data.tar.gz: 0ed1603a9adb971ccd2c7b382a43cb34b1274c99
3
+ metadata.gz: 0a19bb2ea18c1602e43b0a6a7d9b07acc67111ed
4
+ data.tar.gz: 133e19b5b91c09541ffdf4d9bb8cfe26d33ec521
5
5
  SHA512:
6
- metadata.gz: 2679e2dc3b63c14c532e869a61c79949dfc06cb0d398c1b7e8297d755d2500728df7bd716c24a7f3eeb28a3812def9b608ed71e431f4c0bf128c32ac9d25a1e4
7
- data.tar.gz: ffe4c1db890f1089bee3abcd462b4ead7412644f4a4a2f6a0f80f774f95935dcac4ab9b66093344d3c8913c78969fe6f3147ff0423a14d5edd81dddc40c37182
6
+ metadata.gz: 5047b0bb6dab48dafb5466bf8026b1803c6f99250ead35e252d9f9319fbc536579983e3b90b6abb1c02526e37ac84c2d7209f694fb396053fc326a72e2900d6d
7
+ data.tar.gz: 797925e673cb380aa1d0294af8a42ca89b27c303363c027dd5734a3a845d5133f57910d2446c6b8613553ac97b4467f6f7e20ef34012bb3d2c717086dcd46695
@@ -6,4 +6,6 @@ Rakefile
6
6
  lib/bytes.rb
7
7
  lib/bytes/version.rb
8
8
  test/helper.rb
9
+ test/test_bytes.rb
10
+ test/test_hash.rb
9
11
  test/test_string.rb
@@ -1,10 +1,127 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'pp'
4
+ require 'digest'
5
+
4
6
 
5
7
  ## our own code
6
8
  require 'bytes/version' # note: let version always go first
7
9
 
8
10
 
11
+ class Bytes
12
+ def self.new( *args )
13
+ String.new( *args ).b
14
+ end
15
+
16
+ def self.from_hex( hexstr )
17
+ if ['0x', '0X'].include?( hexstr[0...2] )
18
+ [hexstr[2..-1]].pack('H*') ## cut-of leading 0x or 0X if present
19
+ else
20
+ [hexstr].pack('H*')
21
+ end
22
+ end
23
+
24
+ def self.to_hex( str )
25
+ # note: unpack returns string with <Encoding:US-ASCII>
26
+ # conver to default encoding
27
+ ## todo/fix: do NOT hardcode UTF-8 - use default encoding - how?
28
+ str.unpack('H*').first.encode("UTF-8")
29
+ end
30
+
31
+ def self.convert( *args )
32
+ ## used by Bytes() in global Kernel converter method
33
+ if args.size == 1
34
+ if args[0].is_a? Array
35
+ ## assume array of bytes
36
+ ## to be done
37
+ else ## assume String
38
+ ## todo/fix: use coerce to_str if arg is NOT a string - why? why not?
39
+ str = args[0]
40
+ ##
41
+ if str.encoding == Encoding::ASCII_8BIT
42
+ ## assume it's binary data - use as is (no hexstring conversion)
43
+ new( str ) ## todo/check: return str as-is (without new) - why? why not?
44
+ else ## assume it's a hexstring
45
+ from_hex( str )
46
+ end
47
+ end
48
+ else
49
+ ## todo/fix: throw argument error
50
+ end
51
+ end
52
+ end
53
+
54
+
55
+ class Buffer
56
+ def self.new( *args )
57
+ if args.size == 0
58
+ ## note: use "" to always use default encoding (and NOT binary)
59
+ String.new( "" )
60
+ else
61
+ String.new( *args )
62
+ end
63
+ end
64
+ end
65
+
66
+
67
+ module BytesHelper
68
+ def hex_to_bytes( hexstr ) Bytes.from_hex( hexstr); end
69
+ alias_method :h_to_b, :hex_to_bytes
70
+ alias_method :htob, :hex_to_bytes
71
+
72
+ def bytes_to_hex( str ) Bytes.to_hex( str ); end
73
+ alias_method :b_to_h, :bytes_to_hex
74
+ alias_method :btoh, :bytes_to_hex
75
+ end
76
+
77
+
78
+
79
+ class String
80
+ def h_to_b() Bytes.from_hex( self ); end
81
+ alias_method :htob, :h_to_b
82
+
83
+ def b_to_h() Bytes.to_hex( self ); end ## add .b-like shortcut
84
+ alias_method :btoh, :b_to_h
85
+ alias_method :h, :b_to_h
86
+ end
87
+
88
+
89
+ module HashHelper
90
+ def sha256( bytes )
91
+ ## todo/fix: check bytes.encoding - warn if not BINARY!!!!
92
+ Digest::SHA256.digest( bytes )
93
+ end
94
+
95
+ def ripemd160( bytes )
96
+ ## todo/fix: check bytes.encoding - warn if not BINARY!!!!
97
+ Digest::RMD160.digest( bytes )
98
+ end
99
+
100
+ def hash256( bytes )
101
+ ## double - uses sha256(sha256())
102
+ sha256(sha256( bytes ))
103
+ end
104
+
105
+ def hash160( bytes )
106
+ ## double - uses ripemd160(sha256())
107
+ ripemd160(sha256( bytes ))
108
+ end
109
+
110
+ ## convenience shortcut helpers
111
+ def sha256hex( bytes ) Bytes.to_hex(sha256( bytes )); end
112
+ def ripemd160hex( bytes ) Bytes.to_hex(ripemd160( bytes )); end
113
+ def hash256( bytes ) Bytes.to_hex(hash256( bytes )); end
114
+ def hash160( bytes ) Bytes.to_hex(hash160( bytes )); end
115
+ end
116
+
117
+ ## make "global" for now - check if there's a better way (include in Kernel) - why? why ot?
118
+ include HashHelper
119
+ include BytesHelper
120
+
121
+
122
+
123
+ module Kernel
124
+ def Bytes( *args ) Bytes.convert( *args ); end
125
+ end
9
126
 
10
127
  puts Bytes.banner ## say hello
@@ -5,8 +5,8 @@
5
5
  class Bytes
6
6
 
7
7
  MAJOR = 0
8
- MINOR = 0
9
- PATCH = 1
8
+ MINOR = 1
9
+ PATCH = 0
10
10
  VERSION = [MAJOR,MINOR,PATCH].join('.')
11
11
 
12
12
  def self.version
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # to run use
5
+ # ruby -I ./lib -I ./test test/test_bytes.rb
6
+
7
+
8
+ require 'helper'
9
+
10
+
11
+ class TestBytes < MiniTest::Test
12
+
13
+ def test_hex
14
+ assert String.new( "6162") == Bytes.to_hex( "ab" )
15
+ assert String.new( "6162") == bytes_to_hex( "ab" )
16
+ assert String.new( "6162") == "ab".b_to_h
17
+ assert String.new( "6162") == "ab".btoh
18
+ assert String.new( "6162") == "ab".h
19
+ assert String.new( "6162") == Bytes.to_hex( "\x61\x62" )
20
+ assert String.new( "6162") == bytes_to_hex( "\x61\x62" )
21
+ assert String.new( "6162") == "\x61\x62".b_to_h
22
+ assert String.new( "6162") == "\x61\x62".btoh
23
+ assert String.new( "6162") == "\x61\x62".h
24
+ assert Encoding::UTF_8 == Bytes.to_hex( "ab" ).encoding
25
+
26
+ assert Bytes.new( "ab" ) == Bytes.from_hex( "6162" )
27
+ assert Bytes.new( "ab" ) == hex_to_bytes( "6162" )
28
+ assert Bytes.new( "ab" ) == "6162".h_to_b
29
+ assert Bytes.new( "ab" ) == "6162".htob
30
+ assert Bytes.new( "ab" ) == Bytes.from_hex( "0x6162" )
31
+ assert Bytes.new( "ab" ) == hex_to_bytes( "0x6162" )
32
+ assert Bytes.new( "ab" ) == "0x6162".h_to_b
33
+ assert Bytes.new( "ab" ) == "0x6162".htob
34
+ assert Encoding::ASCII_8BIT == Bytes.from_hex( "6162" ).encoding
35
+
36
+ assert Bytes.new( "ab" ) == Bytes( "6162" )
37
+ assert Bytes.new( "ab" ) == Bytes( "0x6162" )
38
+ assert Bytes.new( "6162" ) == Bytes( "6162".b )
39
+ assert Bytes.new( "ab" ) == Bytes( "ab".b )
40
+ assert Bytes.new( "ab") == Bytes( "\x61\x62".b )
41
+ assert Encoding::ASCII_8BIT == Bytes( "6162" ).encoding
42
+ assert Encoding::ASCII_8BIT == Bytes( "6162".b ).encoding
43
+ assert Encoding::ASCII_8BIT == Bytes( "ab".b ).encoding
44
+ end
45
+
46
+ end # class TestBytes
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # to run use
5
+ # ruby -I ./lib -I ./test test/test_hash.rb
6
+
7
+
8
+ require 'helper'
9
+
10
+
11
+ class TestHash < MiniTest::Test
12
+
13
+ def test_hash
14
+ sha256_empty_hex = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
15
+ sha256_empty_bytes = "\xE3\xB0\xC4B\x98\xFC\x1C\x14\x9A\xFB\xF4\xC8\x99o\xB9$'\xAEA\xE4d\x9B\x93L\xA4\x95\x99\exR\xB8U".b
16
+
17
+ assert_equal sha256_empty_bytes, sha256( "" )
18
+ assert_equal sha256_empty_bytes, sha256( Bytes.new )
19
+
20
+ assert_equal sha256_empty_hex, Bytes.to_hex( sha256( "" ) )
21
+ assert_equal sha256_empty_hex, bytes_to_hex( sha256( "" ) )
22
+ assert_equal sha256_empty_hex, btoh( sha256( "" ) )
23
+ assert_equal sha256_empty_hex, sha256( "" ).h
24
+ assert_equal sha256_empty_hex, sha256hex( "" )
25
+
26
+ ripemd160_empty_hex = "9c1185a5c5e9fc54612808977ee8f548b2258d31"
27
+
28
+ assert_equal ripemd160_empty_hex, Bytes.to_hex( ripemd160( "" ))
29
+ assert_equal ripemd160_empty_hex, bytes_to_hex( ripemd160( "" ))
30
+ assert_equal ripemd160_empty_hex, btoh( ripemd160( "" ))
31
+ assert_equal ripemd160_empty_hex, ripemd160( "" ).h
32
+ assert_equal ripemd160_empty_hex, ripemd160hex( "" )
33
+
34
+ assert_equal "37f332f68db77bd9d7edd4969571ad671cf9dd3b",
35
+ ripemd160hex( "The quick brown fox jumps over the lazy dog" )
36
+
37
+ assert_equal "132072df690933835eb8b6ad0b77e7b6f14acad7",
38
+ ripemd160hex( "The quick brown fox jumps over the lazy cog" )
39
+ end
40
+
41
+ end # class TestHash
@@ -29,4 +29,21 @@ class TestString < MiniTest::Test
29
29
  assert Encoding::UTF_8 == String.new("").encoding
30
30
  end
31
31
 
32
+ def test_bytes
33
+ assert Encoding::ASCII_8BIT == Bytes.new.encoding
34
+ assert Encoding::ASCII_8BIT == Bytes.new("").encoding
35
+ assert Encoding::ASCII_8BIT == Bytes.new("ab").encoding
36
+
37
+ assert Encoding::BINARY == Encoding::ASCII_8BIT
38
+ assert Encoding::BINARY == Bytes.new.encoding
39
+ assert Encoding::BINARY == Bytes.new("").encoding
40
+ assert Encoding::BINARY == Bytes.new("ab").encoding
41
+ end
42
+
43
+ def test_buffer
44
+ assert Encoding::UTF_8 == Buffer.new.encoding
45
+ assert Encoding::UTF_8 == Buffer.new("").encoding
46
+ assert Encoding::UTF_8 == Buffer.new("ab").encoding
47
+ end
48
+
32
49
  end # class TestString
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bytes
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
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-15 00:00:00.000000000 Z
11
+ date: 2019-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdoc
@@ -56,6 +56,8 @@ files:
56
56
  - lib/bytes.rb
57
57
  - lib/bytes/version.rb
58
58
  - test/helper.rb
59
+ - test/test_bytes.rb
60
+ - test/test_hash.rb
59
61
  - test/test_string.rb
60
62
  homepage: https://github.com/s6ruby/programming-bits-bytes
61
63
  licenses: