opensecret 0.0.9925 → 0.0.9949

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.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +656 -40
  3. data/lib/configs/README.md +58 -0
  4. data/lib/extension/file.rb +67 -0
  5. data/lib/extension/string.rb +10 -0
  6. data/lib/factbase/facts.opensecret.io.ini +1 -0
  7. data/lib/interprete.rb +334 -61
  8. data/lib/keytools/PRODUCE_RAND_SEQ_USING_DEV_URANDOM.txt +0 -0
  9. data/lib/keytools/kdf.api.rb +9 -15
  10. data/lib/keytools/kdf.bcrypt.rb +69 -19
  11. data/lib/keytools/kdf.pbkdf2.rb +112 -23
  12. data/lib/keytools/key.api.rb +146 -36
  13. data/lib/keytools/key.db.rb +94 -29
  14. data/lib/keytools/key.id.rb +1 -1
  15. data/lib/keytools/key.ident.rb +243 -0
  16. data/lib/keytools/key.local.rb +62 -68
  17. data/lib/keytools/key.pass.rb +2 -2
  18. data/lib/keytools/key.rb +2 -28
  19. data/lib/modules/{cryptology.md → README.md} +0 -0
  20. data/lib/session/fact.finder.rb +65 -428
  21. data/lib/session/time.stamp.rb +1 -28
  22. data/lib/usecase/cmd.rb +127 -54
  23. data/lib/usecase/config/README.md +57 -0
  24. data/lib/usecase/docker/README.md +146 -0
  25. data/lib/usecase/docker/docker.rb +49 -0
  26. data/lib/usecase/edit/README.md +43 -0
  27. data/lib/usecase/edit/delete.rb +46 -0
  28. data/lib/usecase/export.rb +40 -0
  29. data/lib/usecase/files/README.md +37 -0
  30. data/lib/usecase/files/eject.rb +56 -0
  31. data/lib/usecase/files/file_me.rb +78 -0
  32. data/lib/usecase/files/read.rb +169 -0
  33. data/lib/usecase/files/write.rb +89 -0
  34. data/lib/usecase/goto.rb +57 -0
  35. data/lib/usecase/id.rb +1 -1
  36. data/lib/usecase/import.rb +13 -30
  37. data/lib/usecase/init.rb +2 -17
  38. data/lib/usecase/jenkins/README.md +146 -0
  39. data/lib/usecase/jenkins/crazy_ruby_post_attempt.OLD +234 -0
  40. data/lib/usecase/jenkins/jenkins.rb +208 -0
  41. data/lib/usecase/login.rb +6 -5
  42. data/lib/usecase/logout.rb +1 -3
  43. data/lib/usecase/open.rb +11 -66
  44. data/lib/usecase/print.rb +40 -0
  45. data/lib/usecase/put.rb +34 -156
  46. data/lib/usecase/set.rb +2 -4
  47. data/lib/usecase/show.rb +138 -0
  48. data/lib/usecase/terraform/README.md +91 -0
  49. data/lib/usecase/terraform/terraform.rb +121 -0
  50. data/lib/usecase/token.rb +4 -80
  51. data/lib/usecase/update/README.md +55 -0
  52. data/lib/usecase/update/rename.rb +180 -0
  53. data/lib/usecase/use.rb +1 -3
  54. data/lib/usecase/verse.rb +20 -0
  55. data/lib/usecase/view.rb +71 -0
  56. data/lib/usecase/vpn/README.md +150 -0
  57. data/lib/usecase/vpn/vpn.ini +31 -0
  58. data/lib/usecase/vpn/vpn.rb +54 -0
  59. data/lib/version.rb +1 -1
  60. data/opensecret.gemspec +3 -4
  61. metadata +34 -35
  62. data/.travis.yml +0 -5
  63. data/CODE_OF_CONDUCT.md +0 -74
  64. data/LICENSE.txt +0 -21
  65. data/bin/ops +0 -20
  66. data/lib/keytools/binary.map.rb +0 -294
  67. data/lib/keytools/doc.conversion.to.ones.and.zeroes.ruby +0 -179
  68. data/lib/keytools/doc.rsa.radix.binary-mapping.ruby +0 -190
  69. data/lib/keytools/doc.star.schema.strategy.txt +0 -77
  70. data/lib/keytools/doc.using.pbkdf2.kdf.ruby +0 -95
  71. data/lib/keytools/doc.using.pbkdf2.pkcs.ruby +0 -266
  72. data/lib/keytools/key.mach.rb +0 -248
  73. data/lib/keytools/keydebug.txt +0 -295
  74. data/lib/modules/cryptology/open.bcrypt.rb +0 -170
  75. data/lib/usecase/read.rb +0 -89
  76. data/lib/usecase/safe.rb +0 -92
@@ -1,5 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.3.1
5
- before_install: gem install bundler -v 1.16.1
@@ -1,74 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
10
- orientation.
11
-
12
- ## Our Standards
13
-
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
16
-
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
22
-
23
- Examples of unacceptable behavior by participants include:
24
-
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
28
- * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
31
- * Other conduct which could reasonably be considered inappropriate in a
32
- professional setting
33
-
34
- ## Our Responsibilities
35
-
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
39
-
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
45
-
46
- ## Scope
47
-
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
-
55
- ## Enforcement
56
-
57
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at apolloakora@gmail.com. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
-
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
67
-
68
- ## Attribution
69
-
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at [http://contributor-covenant.org/version/1/4][version]
72
-
73
- [homepage]: http://contributor-covenant.org
74
- [version]: http://contributor-covenant.org/version/1/4/
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2018 Apollo Akora
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
data/bin/ops DELETED
@@ -1,20 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'interprete'
4
-
5
- Interprete.start(ARGV)
6
-
7
-
8
- # -- -------------------------- -- #
9
- # -- Other names aside from ops -- #
10
- # -- -------------------------- -- #
11
- # stash
12
- # keep
13
- # safe
14
- # box
15
- # drop
16
- # bag
17
- # db
18
- # devops
19
- # hold (of a ship)
20
- # -- -------------------------- -- #
@@ -1,294 +0,0 @@
1
- #!/usr/bin/ruby
2
-
3
- module OpenKey
4
-
5
- class BinaryMap
6
-
7
-
8
- require 'openssl'
9
- require "base64"
10
- require "bcrypt"
11
-
12
-
13
- CHARACTERS_64 = [
14
- "a", "9", "W", "B", "f", "K", "O", "z",
15
- "3", "s", "1", "5", "c", "n", "E", "J",
16
- "L", "A", "l", "6", "I", "w", "o", "g",
17
- "k", "N", "t", "Y", "S", "/", "T", "b",
18
- "V", "R", "H", "0", "@", "Z", "8", "F",
19
- "G", "j", "u", "m", "M", "h", "4", "p",
20
- "q", "d", "7", "v", "e", "2", "U", "X",
21
- "r", "C", "y", "Q", "D", "x", "P", "i"
22
- ]
23
-
24
- def self.tmp_holder
25
-
26
-
27
- bcrypt_packed_key = [ bcrypt_key.to_s ].pack("B*")
28
- pbkdf2_packed_key = [ pbkdf2_key.to_s ].pack("B*")
29
-
30
- bcrypt_un_pack_ed = bcrypt_packed_key.unpack("B*")[0]
31
- pbkdf2_un_pack_ed = pbkdf2_packed_key.unpack("B*")[0]
32
-
33
- bcrypt_they_match = bcrypt_key.to_s.eql? bcrypt_un_pack_ed
34
- pbkdf2_they_match = pbkdf2_key.to_s.eql? pbkdf2_un_pack_ed
35
-
36
- puts ""
37
- puts "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
38
- puts "The actual binary thing => [ #{bcrypt_key.to_s} ]"
39
- puts "Theactual unpacked ting => [ #{bcrypt_un_pack_ed} ]"
40
- puts "Lengthof packed version => [ #{bcrypt_packed_key.length} ]"
41
- puts "Byte size Length of Pck => [ #{bcrypt_packed_key.bytesize} ]"
42
- puts "The packed keys version => [ #{bcrypt_packed_key} ]"
43
- puts ""
44
- puts "Length unpacked version => [ #{bcrypt_un_pack_ed.length} ]"
45
- puts "Bytesize of Unpacked Vn => [ #{bcrypt_un_pack_ed.bytesize} ]"
46
- puts ""
47
- puts "Is full circle correct? => [ #{bcrypt_they_match} ]"
48
- puts ""
49
- puts bcrypt_key.to_s
50
- puts bcrypt_un_pack_ed
51
- puts ""
52
- puts "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
53
- puts ""
54
- puts "The actual binary thing => [ #{pbkdf2_key.to_s} ]"
55
- puts "Theactual unpacked ting => [ #{pbkdf2_un_pack_ed} ]"
56
- puts "Lengthof packed version => [ #{pbkdf2_packed_key.length} ]"
57
- puts "Byte size Length of Pck => [ #{pbkdf2_packed_key.bytesize} ]"
58
- puts "The packed keys version => [ #{pbkdf2_packed_key} ]"
59
- puts ""
60
- puts "Length unpacked version => [ #{pbkdf2_un_pack_ed.length} ]"
61
- puts "Bytesize of Unpacked Vn => [ #{pbkdf2_un_pack_ed.bytesize} ]"
62
- puts ""
63
- puts "Is full circle correct? => [ #{pbkdf2_they_match} ]"
64
- puts ""
65
- puts pbkdf2_key.to_s
66
- puts pbkdf2_un_pack_ed
67
- puts ""
68
-
69
- char_index = 1
70
- zero_count = 0
71
- one_count = 0
72
-
73
- pbkdf2_key.to_s.each_char do |string_bit|
74
- puts "Bit number [ #{char_index} ] is a [ #{string_bit} ]" if char_index % 4 == 0
75
- char_index += 1
76
- zero_count += 1 if string_bit.eql? "0"
77
- one_count += 1 if string_bit.eql? "1"
78
- end
79
-
80
- puts "There are [ #{zero_count} ] zeroes."
81
- puts "There are [ #{one_count} ] ones."
82
-
83
-
84
-
85
-
86
- end
87
-
88
-
89
-
90
- def self.bcrypter
91
-
92
- generated_salt = BCrypt::Engine.generate_salt( 12 )
93
-
94
-
95
- my_password = BCrypt::Password.new( BCrypt::Engine.hash_secret( "my password text", generated_salt ) )
96
-
97
-
98
- ## my_password = BCrypt::Password.create( "my password", :cost => 12 )
99
-
100
- salt_begin_index = my_password.to_s.rindex( "$" ) + 1
101
- salt_hash_length = my_password.to_s.length - salt_begin_index
102
-
103
-
104
-
105
- #=> "$2a$10$vI8aWBnW3fID.ZQ4/zo1G.q1lRps.9cGLcZEiGDMVr5yUP1KUOYTa"
106
-
107
- # Salt is 22 characters
108
- # Key is 31 characters
109
- # total is 53 characters
110
-
111
- good_password = my_password == "my password text"
112
- bad_password = my_password == "my password text"
113
-
114
- puts ""
115
- puts "Bcrypt password is #{my_password}"
116
- puts "Bcrypt COST is => #{BCrypt::Engine.cost}"
117
- puts "Length of salt plus hash is #{salt_hash_length}"
118
- puts "Salt begin index is #{salt_begin_index}"
119
- puts "Generated Salt is #{generated_salt}"
120
- puts "The Good Password? #{good_password}"
121
- puts "The BadBoy Password? #{bad_password}"
122
- puts ""
123
-
124
-
125
- end
126
-
127
-
128
- ## Bcrypt password is $2a$12$nkyYKCwljFRtcif6FCXn3ey29P8mGMeY6YGNEaoBYmkScgyYrbusC
129
- ## Bcrypt COST is => 10
130
- ## Length of salt plus hash is 53
131
- ## Salt begin index is 7
132
- ## Generated Salt is $2a$12$nkyYKCwljFRtcif6FCXn3e
133
- ## The Good Password? true
134
- ## The BadBoy Password? false
135
-
136
-
137
-
138
- def self.salter
139
-
140
- original_pass = "abcdefghijklmnopqrstuvwxyz@0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ+"
141
-
142
- for n in 0 .. 63
143
- the_pass = original_pass + original_pass[0..n]
144
- puts "==========================================="
145
- puts "Pass #{n} => #{the_pass}"
146
- puts "Password LENGTH => #{the_pass.length}"
147
- puts "==========================================="
148
- password_cruncher the_pass
149
- end
150
-
151
- end
152
-
153
- def self.password_cruncher the_password
154
-
155
-
156
- raise RuntimeError, "Only 72 characters change the resultant key. Strings with 73, 74, 75 (etc) characters produce the same key as the truncated 72 character input." if the_password.length > 72
157
-
158
- preamble = "$2a$12$"
159
- salt_chars = "nkyYKCwljFRtcif6FCXn3e"
160
-
161
- cost_length = "12".length
162
- expected_pre_len = 5 + cost_length
163
- non_key_length = expected_pre_len + 22
164
- raise RuntimeError, "Preamble length not expected" unless preamble.length == expected_pre_len
165
- raise RuntimeError, "Salt length not expected" unless salt_chars.length == 22
166
-
167
- generated_salt = preamble + salt_chars
168
-
169
- my_password = BCrypt::Password.new( BCrypt::Engine.hash_secret( the_password, generated_salt ) ).to_s
170
-
171
- salt_begin_index = my_password.rindex( "$" ) + 1
172
- salt_hash_length = my_password.length - salt_begin_index
173
-
174
- actual_key = my_password.gsub( generated_salt, "" )
175
- raise RuntimeError, "Key length not expected" unless actual_key.length == 31
176
-
177
- also_key = my_password[ non_key_length .. -1 ]
178
- raise RuntimeError, "Key strings not matching" unless actual_key.eql? also_key
179
-
180
-
181
- puts "Bcrypt password is #{my_password}"
182
- puts "Bcrypt COST is => #{BCrypt::Engine.cost}"
183
- puts "Length of salt plus hash is #{salt_hash_length}"
184
- puts "Salt begin index is #{salt_begin_index}"
185
- puts "Generated Salt is #{generated_salt}"
186
- puts "Actual Key 1 => #{actual_key}"
187
- puts "Actual Key 2 => #{also_key}"
188
- puts "KEY Length is => [ #{also_key.length} ]"
189
- puts ""
190
-
191
-
192
- end
193
-
194
-
195
-
196
- def self.crypter
197
-
198
- cipher = OpenSSL::Cipher.new 'AES-128-CBC'
199
- cipher.encrypt
200
- iv = cipher.random_iv
201
-
202
- pwd = 'some hopefully not to easily guessable password'
203
- salt = OpenSSL::Random.random_bytes 16
204
- iter = 20000
205
- key_len = cipher.key_len
206
- digest = OpenSSL::Digest::SHA512.new
207
-
208
- key = OpenSSL::PKCS5.pbkdf2_hmac(pwd, salt, 1000000, 16, digest)
209
-
210
- puts ""
211
- puts "The generated salt has a length of [#{salt.length}]"
212
- puts "The salt generated is => #{salt}"
213
- puts "The generated key has a length of [#{key.length}]"
214
- puts "The Key generated is => #{key}"
215
- puts "The Key in Base64 is => #{Base64.encode64(key)}"
216
- puts ""
217
-
218
- cipher.key = key
219
-
220
- ##### Now encrypt the data:
221
-
222
- ### encrypted = cipher.update document
223
- ### encrypted << cipher.final
224
-
225
- end
226
-
227
-
228
- def self.binary
229
-
230
- for n in 0 .. 128
231
- six_bit_binary = "%06d" % [ n.to_s(2) ]
232
- puts "#{n} in binary is => #{six_bit_binary}"
233
- end
234
-
235
- end
236
-
237
-
238
- def self.binbytes
239
-
240
- for n in 0 .. 256
241
- eight_bit_binary = "%08d" % [ n.to_s(2) ]
242
- puts "#{eight_bit_binary} => #{n}"
243
- end
244
-
245
- end
246
-
247
-
248
- def self.shovel
249
-
250
- first_name = "joe"
251
- last_name = "bloggs"
252
-
253
- user_name = first_name
254
- user_name << last_name
255
-
256
- puts "Username => #{user_name}"
257
- puts "First name => #{first_name}"
258
-
259
- end
260
-
261
-
262
- def self.print_randoms
263
- require 'securerandom'
264
- for n in 0 .. 99
265
- puts "#{from_rand}"
266
- end
267
-
268
- end
269
-
270
-
271
- def self.from_rand
272
- random_bit_string = ""
273
- the_ints = Array.new
274
- for n in 1 .. 48
275
- random_integer = SecureRandom.random_number( 256 )
276
- the_ints.push random_integer
277
- random_bit_string += "%08d" % [ random_integer.to_s(2) ]
278
- end
279
- puts "Integers => #{the_ints.to_s}"
280
- return random_bit_string
281
- end
282
-
283
- ## BinaryMap.print_randoms
284
- ## BinaryMap.shovel
285
- ## BinaryMap.binbytes
286
- ## BinaryMap.binary
287
- ## BinaryMap.crypter
288
- ## BinaryMap.bcrypter
289
- ## BinaryMap.salter
290
-
291
- end
292
-
293
-
294
- end
@@ -1,179 +0,0 @@
1
- # coding: utf-8
2
-
3
- # =====> Look you convert to ones and zeroes like this - see below too.
4
- # =====> Look you convert to ones and zeroes like this - see below too.
5
- # =====> Look you convert to ones and zeroes like this - see below too.
6
- # =====> Look you convert to ones and zeroes like this - see below too.
7
- # =====> Look you convert to ones and zeroes like this - see below too.
8
- # =====> Look you convert to ones and zeroes like this - see below too.
9
- # =====> Look you convert to ones and zeroes like this - see below too.
10
- # =====> Look you convert to ones and zeroes like this - see below too.
11
- # =====> Look you convert to ones and zeroes like this - see below too.
12
- # =====> Look you convert to ones and zeroes like this - see below too.
13
- # =====> Look you convert to ones and zeroes like this - see below too.
14
- # =====> Look you convert to ones and zeroes like this - see below too.
15
-
16
- Conversion from String to binary
17
- Easiest way, using unpack and tell Ruby that it is binary
18
- "Hello".unpack("B*")
19
- => ["0100100001100101011011000110110001101111"]
20
-
21
-
22
- # =====> Look you convert to ones and zeroes like this - see below too.
23
- # =====> Look you convert to ones and zeroes like this - see below too.
24
- # =====> Look you convert to ones and zeroes like this - see below too.
25
- # =====> Look you convert to ones and zeroes like this - see below too.
26
- # =====> Look you convert to ones and zeroes like this - see below too.
27
- # =====> Look you convert to ones and zeroes like this - see below too.
28
- # =====> Look you convert to ones and zeroes like this - see below too.
29
- # =====> Look you convert to ones and zeroes like this - see below too.
30
- # =====> Look you convert to ones and zeroes like this - see below too.
31
- # =====> Look you convert to ones and zeroes like this - see below too.
32
- # =====> Look you convert to ones and zeroes like this - see below too.
33
- # =====> Look you convert to ones and zeroes like this - see below too.
34
- # =====> Look you convert to ones and zeroes like this - see below too.
35
- # =====> Look you convert to ones and zeroes like this - see below too.
36
- # =====> Look you convert to ones and zeroes like this - see below too.
37
- # =====> Look you convert to ones and zeroes like this - see below too.
38
- # =====> Look you convert to ones and zeroes like this - see below too.
39
- # =====> Look you convert to ones and zeroes like this - see below too.
40
- # =====> Look you convert to ones and zeroes like this - see below too.
41
- # =====> Look you convert to ones and zeroes like this - see below too.
42
- # =====> Look you convert to ones and zeroes like this - see below too.
43
- # =====> Look you convert to ones and zeroes like this - see below too.
44
- # =====> Look you convert to ones and zeroes like this - see below too.
45
- # =====> Look you convert to ones and zeroes like this - see below too.
46
- # =====> Look you convert to ones and zeroes like this - see below too.
47
- # =====> Look you convert to ones and zeroes like this - see below too.
48
- # =====> Look you convert to ones and zeroes like this - see below too.
49
- # =====> Look you convert to ones and zeroes like this - see below too.
50
- # =====> Look you convert to ones and zeroes like this - see below too.
51
- # =====> Look you convert to ones and zeroes like this - see below too.
52
- # =====> Look you convert to ones and zeroes like this - see below too.
53
- # =====> Look you convert to ones and zeroes like this - see below too.
54
-
55
-
56
- -100.to_s(2) # => "-1100100"
57
- -100.to_s(2) # => "-1100100"
58
- -100.to_s(2) # => "-1100100"
59
- -100.to_s(2) # => "-1100100"
60
- -100.to_s(2) # => "-1100100"
61
- -100.to_s(2) # => "-1100100"
62
- -100.to_s(2) # => "-1100100"
63
- -100.to_s(2) # => "-1100100"
64
-
65
-
66
-
67
- #### -----> See important padding article
68
- #### -----> See important padding article
69
- #### -----> See important padding article
70
- #### -----> See important padding article
71
- #### -----> See important padding article
72
- #### -----> See important padding article
73
-
74
- ## --> https://stackoverflow.com/questions/4080988/why-does-base64-encoding-require-padding-if-the-input-length-is-not-divisible-by?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
75
-
76
-
77
-
78
-
79
- 2.6. Converting Between Numeric Bases
80
- Problem
81
- You want to convert numbers from one base to another.
82
-
83
- Solution
84
- You can convert specific binary, octal, or hexadecimal numbers to decimal by representing them with the 0b, 0o, or 0x prefixes:
85
-
86
- 0b100 # => 4
87
- 0o100 # => 64
88
- 0x100 # => 256
89
- You can also convert between decimal numbers and string representations of those numbers in any base from 2 to 36. Simply pass the base into String#to_i or Integer#to_s.
90
-
91
- Here are some conversions between string representations of numbers in various bases, and the corresponding decimal numbers:
92
-
93
- "1045".to_i(10) # => 1045
94
- "-1001001".to_i(2) # => -73
95
- "abc".to_i(16) # => 2748
96
- "abc".to_i(20) # => 4232
97
- "number".to_i(36) # => 1442151747
98
- "zz1z".to_i(36) # => 1678391
99
- "abcdef".to_i(16) # => 11259375
100
- "AbCdEf".to_i(16) # => 11259375
101
- Here are some reverse conversions of decimal numbers to the strings that represent those numbers in various bases:
102
-
103
- 42.to_s(10) # => "42"
104
- -100.to_s(2) # => "-1100100"
105
- 255.to_s(16) # => "ff"
106
- 1442151747.to_s(36) # => "number"
107
- Some invalid conversions:
108
-
109
- "6".to_i(2) # => 0
110
- "0".to_i(1) # ArgumentError: illegal radix 1
111
- 40.to_s(37) # ArgumentError: illegal radix 37
112
- Discussion
113
- String#to_i can parse and Integer#to_s can create a string representation in every common integer base: from binary (the familiar base 2, which uses only the digits 0 and 1) to hexatridecimal (base 36). Hexatridecimal uses the digits 0–9 and the letters a–z; it's sometimes used to generate alphanumeric
114
-
115
-
116
-
117
-
118
-
119
-
120
- Convert String to Binary in Ruby
121
- As for the sake of today 11/11/11,
122
- Everything is in binary mode.
123
- Why not do a conversion between String and binary :)
124
-
125
- Conversion from String to binary
126
- Easiest way, using unpack and tell Ruby that it is binary
127
- "Hello".unpack("B*")
128
- => ["0100100001100101011011000110110001101111"]
129
-
130
- You can specify length of each binary by replace * with total length of the word (default binary has 8 bit per character)
131
- "Hello".unpack("B40")
132
- => ["0100100001100101011011000110110001101111"]
133
-
134
- The output is in array format, removing an array can be done by adding [0] to select the first element from array.
135
- "Hello".unpack("B*")[0]
136
- => "0100100001100101011011000110110001101111"
137
-
138
- Other method is to break down the string into characters first and get ASCII number of each character, then convert them to binary.
139
- Here is a way for breaking a String into ASCII character code.
140
- ‎"Hello".unpack("C*")
141
- => [72, 101, 108, 108, 111]
142
-
143
- Then using print formatting with %b to convert each ASCII code (int) to binary
144
- "Hello".unpack("C*").each{|c| print "%08b" % c}
145
-
146
- Another way to do using split('')
147
- "Hello".split('').each{|c| print "%08b" % c[0] }
148
- => 0100100001100101011011000110110001101111
149
-
150
- Or you could use scan(/./)
151
- "Hello".scan(/./).each{|c| print "%08b" % c[0] }
152
- => 0100100001100101011011000110110001101111
153
-
154
- Actually there is another method for converting number into binary, it is using ".to_str(2)" but this method doesn't put leading-zeros in the result, as you can see:
155
- "Hello".scan(/./).each{|c| print "%08s " % c[0].to_s(2) }
156
- => 1001000 1100101 1101100 1101100 1101111
157
-
158
- Conversion from binary back to String
159
- Same as above, we can use the same method to convert binary back to String
160
- a="0100100001100101011011000110110001101111"
161
- [a].pack("B*")
162
- => "Hello"
163
-
164
- Or split it by 8 digit each, then convert each 8 digit to int , then to character
165
- a="0100100001100101011011000110110001101111"
166
- (0..a.length).step(8).each {|i| print a[i,8].to_i(2).chr}
167
- => Hello
168
-
169
-
170
-
171
- P.S.
172
- After reading .pack and .unpack function API, I found that Ruby has a built-in Base64 encoder/decoder.
173
- To use it just use pack() or unpack() with "m" directive
174
- Base64 Encoding
175
- ["Hello"].pack("m*").chomp
176
- => "SGVsbG8="
177
- Base64 Decoding
178
- "SGVsbG8=".unpack("m*")[0]
179
- => "Hello"