├── VERSION ├── .gitignore ├── CHANGELOG ├── MIT-LICENSE ├── base58.gemspec ├── Rakefile ├── lib └── base58.rb ├── README.md └── test └── test_base58.rb /VERSION: -------------------------------------------------------------------------------- 1 | 0.2.3 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | rdoc 2 | *.gem 3 | .DS_Store -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | = 0.2.3 [2018-05-16] 2 | - Add licence information to Gemspec. 3 | 4 | = 0.2.2 [2017-01-11] 5 | - Fix for incorrect encoding of leading-zeroes. Thanks to @joelnordell. 6 | 7 | = 0.2.1 [2017-01-08] 8 | - Bitcoin-style leading-zeroes encoding. Thanks to @joelnordell. 9 | 10 | = 0.2.0 [2017-01-05] 11 | - Bitcoin and Ripple alphabet support. Thanks to @joelnordell. 12 | - Binary encoding and decoding support. Thanks to @joelnordell. 13 | 14 | = 0.1.0 [2010-02-05] 15 | - Better argument checking. Thanks to @jimeh. 16 | - Add `encode` and `decode` aliases. 17 | 18 | = 0.0.4 [2009-11-27] 19 | - Gracefully deals with bad input. Thanks to @isnotvalid. 20 | 21 | = 0.0.3 [2009-11-05] 22 | - Initial release. 23 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009 - 2018 Douglas F Shearer 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /base58.gemspec: -------------------------------------------------------------------------------- 1 | # Generated by jeweler 2 | # DO NOT EDIT THIS FILE DIRECTLY 3 | # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' 4 | # -*- encoding: utf-8 -*- 5 | # stub: base58 0.2.3 ruby lib 6 | 7 | Gem::Specification.new do |s| 8 | s.name = "base58".freeze 9 | s.version = "0.2.3" 10 | 11 | s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= 12 | s.require_paths = ["lib".freeze] 13 | s.authors = ["Douglas F Shearer".freeze] 14 | s.date = "2018-05-16" 15 | s.description = "Base58 is a Ruby library for converting ints or binaries to and from base58.".freeze 16 | s.email = "dougal.s@gmail.com".freeze 17 | s.extra_rdoc_files = [ 18 | "README.md" 19 | ] 20 | s.files = [ 21 | "CHANGELOG", 22 | "MIT-LICENSE", 23 | "README.md", 24 | "Rakefile", 25 | "VERSION", 26 | "base58.gemspec", 27 | "lib/base58.rb", 28 | "test/test_base58.rb" 29 | ] 30 | s.homepage = "http://github.com/dougal/base58".freeze 31 | s.licenses = ["MIT".freeze] 32 | s.rubygems_version = "2.7.6".freeze 33 | s.summary = "Base58 is a Ruby library for converting ints or binaries to and from base58.".freeze 34 | end 35 | 36 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rake' 2 | require 'rake/testtask' 3 | require 'rdoc/task' 4 | 5 | desc 'Default: run unit tests.' 6 | task :default => :test 7 | 8 | desc 'Test the base58 library.' 9 | Rake::TestTask.new do |t| 10 | t.libs << 'lib' 11 | t.pattern = 'test/**/test_*.rb' 12 | t.verbose = false 13 | end 14 | 15 | desc 'Generate RDoc documentation.' 16 | Rake::RDocTask.new do |rdoc| 17 | rdoc.rdoc_dir = 'rdoc' 18 | rdoc.title = 'base58' 19 | rdoc.options << '--line-numbers' << '--inline-source' 20 | rdoc.rdoc_files.include('README.md') 21 | rdoc.rdoc_files.include('lib/**/*.rb') 22 | end 23 | 24 | begin 25 | require 'jeweler' 26 | Jeweler::Tasks.new do |gemspec| 27 | gemspec.name = "base58" 28 | gemspec.summary = "Base58 is a Ruby library for converting ints or binaries to and from base58." 29 | gemspec.description = gemspec.summary 30 | gemspec.email = "dougal.s@gmail.com" 31 | gemspec.homepage = "http://github.com/dougal/base58" 32 | gemspec.authors = ["Douglas F Shearer"] 33 | gemspec.licenses = ['MIT'] 34 | end 35 | Jeweler::GemcutterTasks.new 36 | rescue LoadError 37 | puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com" 38 | end 39 | -------------------------------------------------------------------------------- /lib/base58.rb: -------------------------------------------------------------------------------- 1 | # Base58 2 | # Copyright (c) 2009 - 2018 Douglas F Shearer. 3 | # http://douglasfshearer.com 4 | # Distributed under the MIT license as included with this plugin. 5 | 6 | class Base58 7 | 8 | # See https://en.wikipedia.org/wiki/Base58 9 | ALPHABETS = { 10 | flickr: "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ", # This is the default 11 | bitcoin: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz", # Also used for IPFS 12 | ripple: "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz" 13 | } 14 | 15 | # NOTE: If adding new alphabets of non-standard length, this should become a method. 16 | BASE = ALPHABETS[:flickr].length 17 | 18 | # Converts a base58 string to a base10 integer. 19 | def self.base58_to_int(base58_val, alphabet = :flickr) 20 | raise ArgumentError, 'Invalid alphabet selection.' unless ALPHABETS.include?(alphabet) 21 | int_val = 0 22 | base58_val.reverse.split(//).each_with_index do |char,index| 23 | raise ArgumentError, 'Value passed not a valid Base58 String.' if (char_index = ALPHABETS[alphabet].index(char)).nil? 24 | int_val += (char_index)*(BASE**(index)) 25 | end 26 | int_val 27 | end 28 | 29 | # Converts a base10 integer to a base58 string. 30 | def self.int_to_base58(int_val, alphabet = :flickr) 31 | raise ArgumentError, 'Value passed is not an Integer.' unless int_val.is_a?(Integer) 32 | raise ArgumentError, 'Invalid alphabet selection.' unless ALPHABETS.include?(alphabet) 33 | base58_val = '' 34 | while(int_val >= BASE) 35 | mod = int_val % BASE 36 | base58_val = ALPHABETS[alphabet][mod,1] + base58_val 37 | int_val = (int_val - mod)/BASE 38 | end 39 | ALPHABETS[alphabet][int_val,1] + base58_val 40 | end 41 | 42 | # Converts a ASCII-8BIT (binary) encoded string to a base58 string. 43 | def self.binary_to_base58(binary_val, alphabet = :flickr, include_leading_zeroes = true) 44 | raise ArgumentError, 'Value passed is not a String.' unless binary_val.is_a?(String) 45 | raise ArgumentError, 'Value passed is not binary.' unless binary_val.encoding == Encoding::BINARY 46 | raise ArgumentError, 'Invalid alphabet selection.' unless ALPHABETS.include?(alphabet) 47 | return int_to_base58(0, alphabet) if binary_val.empty? 48 | 49 | if include_leading_zeroes 50 | nzeroes = binary_val.bytes.find_index{|b| b != 0} || binary_val.length-1 51 | prefix = ALPHABETS[alphabet][0] * nzeroes 52 | else 53 | prefix = '' 54 | end 55 | 56 | prefix + int_to_base58(binary_val.unpack('H*')[0].to_i(16), alphabet) 57 | end 58 | 59 | # Converts a base58 string to an ASCII-8BIT (binary) encoded string. 60 | # All leading zeroes in the base58 input are preserved and converted to 61 | # "\x00" in the output. 62 | def self.base58_to_binary(base58_val, alphabet = :flickr) 63 | raise ArgumentError, 'Invalid alphabet selection.' unless ALPHABETS.include?(alphabet) 64 | nzeroes = base58_val.chars.find_index{|c| c != ALPHABETS[alphabet][0]} || base58_val.length-1 65 | prefix = nzeroes < 0 ? '' : '00' * nzeroes 66 | [prefix + Private::int_to_hex(base58_to_int(base58_val, alphabet))].pack('H*') 67 | end 68 | 69 | module Private 70 | def self.int_to_hex int 71 | hex = int.to_s(16) 72 | # The hex string must always consist of an even number of characters, 73 | # otherwise the pack() parsing will be misaligned. 74 | (hex.length % 2 == 0) ? hex : ('0'+hex) 75 | end 76 | end 77 | 78 | class << self 79 | alias_method :encode, :int_to_base58 80 | alias_method :decode, :base58_to_int 81 | end 82 | 83 | end 84 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Base58 2 | 3 | Ruby gem for encoding/decoding integers to/from Base58. Supports Flickr, Bitcoin, and Ripple alphabets. 4 | 5 | 6 | ## Usage 7 | 8 | Converting an integer into a Base58 string: 9 | 10 | ```ruby 11 | Base58.int_to_base58(12345) # => "4ER" 12 | ``` 13 | 14 | Converting a Base58 string to the represented integer: 15 | 16 | ```ruby 17 | Base58.base58_to_int("A2Ph") # => 6639914 18 | ``` 19 | 20 | Converting binary into a Base58 string: 21 | 22 | ```ruby 23 | Base58.binary_to_base58("\xCE\xE99\x86".force_encoding('BINARY')) # => "6hKMCS" 24 | ``` 25 | 26 | Converting a Base58 string to the represented binary: 27 | 28 | ```ruby 29 | Base58.base58_to_binary("6hKMCS") # => "\xCE\xE99\x86" 30 | ``` 31 | 32 | 33 | ## Installation 34 | 35 | Install `base58` with the following command 36 | 37 | ```bash 38 | gem install base58 39 | ``` 40 | 41 | Or add it to your Gemfile 42 | 43 | ```ruby 44 | gem 'base58' 45 | ``` 46 | 47 | Then run `bundle install`. 48 | 49 | 50 | ## What is Base58? 51 | 52 | From [Wikipedia](https://en.wikipedia.org/wiki/Base58): 53 | 54 | > Base58 is a group of binary-to-text encoding schemes used to represent large integers as alphanumeric text. It is similar to Base64 but has been modified to avoid both non-alphanumeric characters and letters which might look ambiguous when printed. It is therefore designed for human users who manually enter the data, copying from some visual source, but also allows easy copy and paste because a double-click will usually select the whole string. 55 | 56 | Base58 alphabets are made up of the characters a-z, A-Z, and 0-9, with visually ambiguous characters (0, O, I, l) removed. 57 | 58 | 59 | ## Supported Alphabets 60 | 61 | This library supports three of the most common Base58 alphabets, which have identical, but differently sorted characters. 62 | 63 | Alphabets can be selected by passing a symbol to the second argument of `Base58.int_to_base58` and `Base58.base58_to_int`. 64 | 65 | ```ruby 66 | Base58.int_to_base58(12345, :bitcoin) 67 | ``` 68 | 69 | `:flickr` is the default if no second argument is passed. 70 | 71 | 72 | ### Flickr 73 | 74 | Identifier: `:flickr` 75 | 76 | This is the default alphabet. Used to generate [Flickr short URLs](https://www.flickr.com/groups/api/discuss/72157616713786392/). The order of it's characters is numeric, lowercase-alpha, uppercase-alpha. 77 | 78 | 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ 79 | 80 | 81 | ### Bitcoin 82 | 83 | Identifier: `:bitcoin` 84 | 85 | The alphabet used by the [Bitcoin](https://en.wikipedia.org/wiki/Bitcoin) protocol. The order of it's characters is numeric, uppercase-alpha, lowercase-alpha. 86 | 87 | 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz 88 | 89 | See the "Leading Zeros" section for enabling full Bitcoin-style leading-zeros support. 90 | 91 | 92 | ### Ripple 93 | 94 | Identifier: `:ripple` 95 | 96 | The alphabet used by the [Ripple protocol](https://en.wikipedia.org/wiki/Ripple_(payment_protocol)). The order of the characters were chosen such that the [low values match the primitives of the protocol](http://bitcoin.stackexchange.com/questions/14124/why-is-ripples-base58-alphabet-so-weird). 97 | 98 | rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz 99 | 100 | 101 | ## Leading Zeroes 102 | 103 | Some protocols, such as Bitcoin, require that leading zeros be encoded. Passing `true` to the third argument of binary_to_base58()` will enable this behaviour. 104 | 105 | ```ruby 106 | bitcoin_address_hex = '00000000000000000000123456789ABCDEF0' 107 | bitcoin_address_bin = [bitcoin_address_hex].pack('H*') 108 | 109 | Base58.binary_to_base58(bitcoin_address_bin, :bitcoin, true) # => 111111111143c9JGph3DZ 110 | ``` 111 | 112 | ## Contributing 113 | 114 | Source repository is on [Github](https://github.com/dougal/base58), please file issues and pull requests there. 115 | 116 | 117 | ## Documentation 118 | 119 | Documentation can be found online at [RubyDoc.info](http://www.rubydoc.info/github/dougal/base58). 120 | 121 | Alternatively, you can generate docs from the project root with: 122 | 123 | ```bash 124 | rake rdoc 125 | ``` 126 | 127 | 128 | ## Credits 129 | 130 | * [Joel Nordell](https://github.com/joelnordell) for various Bitcoin/Ripple support features. 131 | * [Jim Myhrberg](https://github.com/jimeh) for argument checking and method aliases. 132 | 133 | 134 | ## Copyright and Licence 135 | 136 | Copyright (c) 2009 - 2018, Douglas F Shearer. 137 | 138 | Base58 is licensed under the MIT Licence. 139 | -------------------------------------------------------------------------------- /test/test_base58.rb: -------------------------------------------------------------------------------- 1 | require 'test/unit' 2 | require 'base58' 3 | 4 | class TestBase58 < Test::Unit::TestCase 5 | 6 | EXAMPLES = { 7 | :flickr => { 8 | "6hKMCS" => 3471391110, 9 | "6hDrmR" => 3470152229, 10 | "6hHHZB" => 3470988633, 11 | "6hHKum" => 3470993664, 12 | "6hLgFW" => 3471485480, 13 | "6hBRKR" => 3469844075, 14 | "6hGRTd" => 3470820062, 15 | "6hCuie" => 3469966999, 16 | "6hJuXN" => 3471139908, 17 | "6hJsyS" => 3471131850, 18 | "6hFWFb" => 3470641072, 19 | "6hENdZ" => 3470417529, 20 | "6hEJqg" => 3470404727, 21 | "6hGNaq" => 3470807546, 22 | "6hDRoZ" => 3470233089, 23 | "6hKkP9" => 3471304242, 24 | "6hHVZ3" => 3471028968, 25 | "6hNcfE" => 3471860782, 26 | "6hJBqs" => 3471161638, 27 | "6hCPyc" => 3470031783, 28 | "6hJNrC" => 3471198710, 29 | "6hKmkd" => 3471305986, 30 | "6hFUYs" => 3470635346, 31 | "6hK6UC" => 3471257464, 32 | "6hBmiv" => 3469744991, 33 | "6hKex1" => 3471283122, 34 | "6hFHQj" => 3470597870, 35 | "6hCA2n" => 3469986263, 36 | "6hBTgt" => 3469849157, 37 | "6hHEss" => 3470976734, 38 | "6hLows" => 3471508478, 39 | "6hD95z" => 3470094097, 40 | "6hKjcq" => 3471298806, 41 | "6hGEbd" => 3470780680, 42 | "6hKSNS" => 3471408510, 43 | "6hG8hv" => 3470676761, 44 | "6hEmj6" => 3470330361, 45 | "6hGjpn" => 3470714163, 46 | "6hEsUr" => 3470352537, 47 | "6hJEhy" => 3471171272, 48 | "6hKBHn" => 3471357731, 49 | "6hG3gi" => 3470659871, 50 | "6hFJTT" => 3470601441, 51 | "6hLZDs" => 3471626624, 52 | "6hGdL7" => 3470695182, 53 | "6hBpi4" => 3469755057, 54 | "6hEuFV" => 3470358539, 55 | "6hGVw1" => 3470832288, 56 | "6hLdm1" => 3471474232, 57 | "6hFcCK" => 3470496279, 58 | "6hDZmR" => 3470259877, 59 | "6hG8iX" => 3470676845, 60 | "6hFZZL" => 3470652242, 61 | "6hJ79u" => 3471063156, 62 | "6hMsrS" => 3471716780, 63 | "6hGH3G" => 3470790336, 64 | "6hKqD3" => 3471320476, 65 | "6hKxEY" => 3471344136, 66 | "6hHVF1" => 3471027922, 67 | "6hKDSQ" => 3471365008, 68 | "6hKwHs" => 3471340916, 69 | "6hH4s6" => 3470858973, 70 | "6hGmKB" => 3470722065, 71 | "6hEzdi" => 3470373757, 72 | "6hJQwJ" => 3471205734, 73 | "6hHd6a" => 3470888035, 74 | "6hH1j3" => 3470848414, 75 | "6hNP1u" => 3471981064, 76 | "6hFWge" => 3470639683, 77 | "6hFpmP" => 3470535723, 78 | "6hCgQZ" => 3469925109, 79 | "6hFJSm" => 3470601352, 80 | "6hEd9v" => 3470302893, 81 | "6hDwuF" => 3470169503, 82 | "6hCVSX" => 3470053055, 83 | "6hCUgr" => 3470047631, 84 | "6hEsqR" => 3470350937, 85 | "6hBmKg" => 3469746485, 86 | "6hDvUx" => 3470167523, 87 | "6hJUi7" => 3471218400, 88 | "6hF39e" => 3470464349, 89 | "6hH43K" => 3470857619, 90 | "6hGSC5" => 3470822548, 91 | "6hFz1s" => 3470568182, 92 | "6hFaKZ" => 3470489971, 93 | "6hD65K" => 3470084015, 94 | "6hBAVk" => 3469794165, 95 | "6hLkWA" => 3471499786, 96 | "6hHi7q" => 3470904928, 97 | "6hHdDF" => 3470889921, 98 | "6hHcCo" => 3470886482, 99 | "6hGQQf" => 3470816526, 100 | "6hLAfo" => 3471547914, 101 | "6hBDEV" => 3469803421, 102 | "6hL4BE" => 3471444864, 103 | "6hL2TT" => 3471439077, 104 | "6hKcxb" => 3471276404, 105 | "6hD1vg" => 3470068617, 106 | "6hLtTT" => 3471526541, 107 | "6hHXtw" => 3471033984, 108 | "6hHCQj" => 3470971274, 109 | "6hFrXx" => 3470544465, 110 | "6hMVkJ" => 3471807252, 111 | "6hDv6V" => 3470164819, 112 | "6hD1gR" => 3470067839, 113 | "6hShWW" => 3472660386, 114 | "6hK4tb" => 3471249260, 115 | "6hLzrQ" => 3471545214, 116 | "6hBTAe" => 3469850245, 117 | "6hLABq" => 3471549134, 118 | "6hGbN7" => 3470688570, 119 | "6hFtro" => 3470549444, 120 | "6hRRAQ" => 3472575120, 121 | "6hFViL" => 3470636466, 122 | "6hFkLP" => 3470523659, 123 | "6hNAKc" => 3471939809, 124 | "6hLLNE" => 3471583426, 125 | "6hJstp" => 3471131533, 126 | "6hHxv3" => 3470953336, 127 | "6hLToQ" => 3471605592, 128 | "6hJ74F" => 3471062877, 129 | "6hGjCA" => 3470714930, 130 | "6hCQoD" => 3470034593, 131 | "6hCqxX" => 3469954397, 132 | "6hCbg8" => 3469906325, 133 | "6hJwGw" => 3471145750, 134 | "6hP2Tt" => 3472024389, 135 | "6hHDuy" => 3470973492, 136 | "6hGRpq" => 3470818450, 137 | "6hDx8F" => 3470171649, 138 | "6hLhxU" => 3471488378, 139 | "6hFrkd" => 3470542358, 140 | "6hPc3D" => 3472055197, 141 | "6hJV29" => 3471220838, 142 | "6hCc3c" => 3469908939, 143 | "6hLycA" => 3471541024, 144 | "6hLd75" => 3471473424, 145 | "6hKJ1m" => 3471378900, 146 | "6hHgEG" => 3470900072, 147 | "6hFNfm" => 3470612720, 148 | "6hFsaF" => 3470545169, 149 | "6hERqV" => 3470428313, 150 | "6hEnYK" => 3470335967, 151 | "6hDGeT" => 3470202285, 152 | "6hFDZo" => 3470584940, 153 | "6hMvPE" => 3471728136, 154 | "6hKTro" => 3471410628, 155 | "6hKfXG" => 3471287918, 156 | "6hKeuU" => 3471283000, 157 | "6hHFYj" => 3470981830, 158 | "6hHDzj" => 3470973768, 159 | "6hCozt" => 3469947757, 160 | "6hKB8D" => 3471355775, 161 | "6hCtrc" => 3469964097, 162 | "6hDXcx" => 3470252609, 163 | "6hCxSR" => 3469979041, 164 | "6hC1Vk" => 3469874901, 165 | "6hKmaS" => 3471305444, 166 | "6hK9fn" => 3471265337, 167 | "6hFDH6" => 3470583995, 168 | "6hEB7c" => 3470380131, 169 | "6hC1E2" => 3469874013, 170 | "6hBZnx" => 3469869693, 171 | "6hBXNz" => 3469864417, 172 | "6hQKjm" => 3472358868, 173 | "6hHn4j" => 3470918204, 174 | "6hHiQ2" => 3470907341, 175 | "6hHhHb" => 3470903580, 176 | "6hGnBc" => 3470724941, 177 | "6hG6Ht" => 3470671481, 178 | "6hDvh6" => 3470165409, 179 | "6hCGtp" => 3470007957, 180 | "6hCnzi" => 3469944383, 181 | "6hMxEY" => 3471734360, 182 | "6hG9sL" => 3470680720, 183 | "6hCarn" => 3469903555, 184 | "6hLsdE" => 3471520902, 185 | "6hKnDa" => 3471310391, 186 | "6hKn2L" => 3471308338, 187 | "6hGpfH" => 3470730481, 188 | "6hRkJS" => 3472474666, 189 | "6hFEV3" => 3470588052, 190 | "6hE7VV" => 3470285343, 191 | "6hSSAq" => 3472773572, 192 | "6hNTtQ" => 3471996106, 193 | "6hMAuK" => 3471743859, 194 | "6hJ95H" => 3471069665, 195 | "6hHZ39" => 3471039240, 196 | "6hByNi" => 3469787029, 197 | "6hLLnb" => 3471581948, 198 | "6hHYoQ" => 3471037076, 199 | "6hHCLm" => 3470971044, 200 | "6hFHkC" => 3470596206, 201 | "6hDKq4" => 3470212967, 202 | "6hRapC" => 3472439910, 203 | "6hKJBs" => 3471380936, 204 | "6hHids" => 3470905278, 205 | "6hEJ8R" => 3470403775, 206 | "6hMY3L" => 3471816360, 207 | "6hFRAC" => 3470623988, 208 | "6hEP9c" => 3470420615, 209 | "6hEqVR" => 3470345891, 210 | "6hHGBX" => 3470984013, 211 | "6hEzFB" => 3470375341, 212 | "6hDnRp" => 3470140429, 213 | "6hDdQH" => 3470110113, 214 | "6hCK7B" => 3470016843, 215 | "6hCxvH" => 3469977815, 216 | "6hC4v4" => 3469883585, 217 | "6hC15g" => 3469872055, 218 | "6hGHRA" => 3470793056, 219 | "6hGCGL" => 3470775724, 220 | "6hGbuW" => 3470687574, 221 | "6hT7FY" => 3472820990, 222 | "6hMFHs" => 3471761416, 223 | "6hJybH" => 3471150749, 224 | "6hGEFs" => 3470782376, 225 | "6hCBnX" => 3469990821, 226 | "6hNJZt" => 3471967549, 227 | "6hMxUV" => 3471735169, 228 | "6hLoGG" => 3471509072, 229 | "6hJdy5" => 3471084708, 230 | "6hGnwp" => 3470724663, 231 | "6hGkhZ" => 3470717157, 232 | "6hG7yd" => 3470674308, 233 | "6hDAqF" => 3470182727, 234 | "6hPQVJ" => 3472182628, 235 | "6hHyqy" => 3470956440, 236 | "6hFG6k" => 3470592013, 237 | "6hTavC" => 3472830482, 238 | "6hJjzU" => 3471104998, 239 | "6hFE7r" => 3470585349, 240 | "6hNQU7" => 3471987422, 241 | "6hJYSj" => 3471233782, 242 | "6hFVRB" => 3470638313, 243 | "6hEeQt" => 3470308575, 244 | "6hBmnK" => 3469745237, 245 | "6hP9VU" => 3472048078, 246 | "6hJeDp" => 3471088381, 247 | "6hHV4d" => 3471025846, 248 | "6hFXmS" => 3470643374, 249 | "6hBgEn" => 3469729381, 250 | "6hNDjB" => 3471948475, 251 | "6hKEkd" => 3471366538, 252 | "6hJDq6" => 3471168345, 253 | "6hHbCG" => 3470883136, 254 | "6hCgN2" => 3469924937, 255 | "6hQa3S" => 3472243594, 256 | "6hLphv" => 3471511033, 257 | "6hHoqd" => 3470922780, 258 | "6hGT2W" => 3470823932, 259 | "6hEd6V" => 3470302743, 260 | "6hNac3" => 3471853844, 261 | "6hHzYe" => 3470961641, 262 | "6hRDAC" => 3472534740, 263 | "6hJ2bu" => 3471046452, 264 | "6hGRPN" => 3470819864, 265 | "6hFS6P" => 3470625681, 266 | "6hG8Yn" => 3470679073, 267 | "6hFSGB" => 3470627699, 268 | "6hFQhL" => 3470619588, 269 | "6hF4VT" => 3470470361, 270 | "6hE7vD" => 3470283935, 271 | "6hBeKa" => 3469722931, 272 | "6hPLs1" => 3472167564, 273 | "6hHcKm" => 3470886886, 274 | "6hG9KW" => 3470681716, 275 | "6hE8E4" => 3470287787, 276 | "6hNp1U" => 3471900352, 277 | "6hJ29T" => 3471046359, 278 | "6hHPLb" => 3471008038, 279 | "6hGWGq" => 3470836256, 280 | "6hEipV" => 3470320607, 281 | "6hMB8U" => 3471746014, 282 | "6hKWyr" => 3471421129, 283 | "6hKLxb" => 3471387416, 284 | "6hJstE" => 3471131548, 285 | "6hHk3k" => 3470911419, 286 | "6hPSdE" => 3472186974, 287 | "6hPfEY" => 3472067396, 288 | "6hGVSS" => 3470833498, 289 | "6hFVX4" => 3470638629, 290 | "6hFQRa" => 3470621467, 291 | "6hCsKK" => 3469961809, 292 | "6hJbdY" => 3471076872, 293 | "6hE2ok" => 3470266691, 294 | "6hCrc8" => 3469956553, 295 | "6hRgwS" => 3472460514, 296 | "6hPhLY" => 3472074472, 297 | "6hLTK1" => 3471606762, 298 | "6hHh5u" => 3470901452, 299 | "6hDiBB" => 3470126173, 300 | "6hD678" => 3470084095, 301 | "6hKMXC" => 3471392198, 302 | "6hBohi" => 3469751649, 303 | "6hJXRV" => 3471230395, 304 | "6hFzad" => 3470568690, 305 | "6hCCbH" => 3469993533, 306 | "6hLpoA" => 3471511386, 307 | "6hKZQN" => 3471432170, 308 | "6hG3Ax" => 3470660987, 309 | "6hT7kf" => 3472819788, 310 | "6hKrzq" => 3471323630, 311 | "6hHMHM" => 3471001171, 312 | "6hHL9q" => 3470995872, 313 | "6hBUkR" => 3469852775, 314 | "6hRRLf" => 3472575666, 315 | "6hJFUg" => 3471176707, 316 | "6hBML2" => 3469830629, 317 | "6hS9eE" => 3472631080, 318 | "6hPD3o" => 3472142646, 319 | "6hKWhL" => 3471420220, 320 | "6hJMpr" => 3471195219, 321 | "6hHzVA" => 3470961488, 322 | "6hGvu9" => 3470751444, 323 | "6hGkTu" => 3470719158, 324 | "6hF7bv" => 3470477937, 325 | "6hDzQp" => 3470180739, 326 | "6hQ2Mo" => 3472219148, 327 | "6hM7Tn" => 3471650979, 328 | "6hJWDp" => 3471226305, 329 | "6hJpX5" => 3471123046, 330 | "6hNLTn" => 3471973923, 331 | "6hGuRu" => 3470749318, 332 | "6hG1CM" => 3470654389, 333 | "6hEMDx" => 3470415589, 334 | "6hCYpx" => 3470061557, 335 | "6hCSTr" => 3470042991, 336 | "6hBnJr" => 3469749801, 337 | "6hRZSh" => 3472602928, 338 | "6hRtVW" => 3472502220, 339 | "6hDzc2" => 3470178571, 340 | "6hCMan" => 3470023731, 341 | "6hRfUj" => 3472458394, 342 | "6hLdME" => 3471475720, 343 | "6hE69P" => 3470279363, 344 | "6hDgaa" => 3470117911, 345 | "6hDeKg" => 3470113161, 346 | "6hKSis" => 3471406804, 347 | "6hKwYj" => 3471341778, 348 | "6hJ4Ro" => 3471055436, 349 | "6hHTug" => 3471020571, 350 | "6hHSH2" => 3471017947, 351 | "6hHCE6" => 3470970681, 352 | "6hG5R5" => 3470668558, 353 | "6hFaBX" => 3470489505, 354 | "6hL13o" => 3471432842, 355 | "6hJtQr" => 3471136117, 356 | "6hHpg3" => 3470925612, 357 | "6hGinv" => 3470710691, 358 | "6hFQXR" => 3470621855, 359 | "6hCKN6" => 3470019133, 360 | "6hJme9" => 3471110522, 361 | "6hGUY8" => 3470830439, 362 | "6hEDPM" => 3470389271, 363 | "6hBg1c" => 3469727167, 364 | "6hNyoj" => 3471931870, 365 | "6hNdQu" => 3471866108, 366 | "6hMNAK" => 3471784575, 367 | "6hHkvV" => 3470913019, 368 | "6hDHLi" => 3470207413, 369 | "6hCwUk" => 3469975763, 370 | "6hCd2a" => 3469912243, 371 | "6hPeRd" => 3472064626, 372 | "6hP4SL" => 3472031076, 373 | "6hJQFC" => 3471206250, 374 | "6hJLVJ" => 3471193612, 375 | "6hJAJT" => 3471159343, 376 | "6hJ8Mi" => 3471068655, 377 | "6hJ6o5" => 3471060580, 378 | "6hH667" => 3470864484, 379 | "6hH5RU" => 3470863718, 380 | "6hC2jx" => 3469876247, 381 | "6hPm75" => 3472085672, 382 | "6hN8ud" => 3471848112, 383 | "6hLD4g" => 3471557361, 384 | "6hGAr2" => 3470768083, 385 | "6hFVQH" => 3470638261, 386 | "6hDxtV" => 3470172823, 387 | "6hPNwj" => 3472174542, 388 | "6hKTB7" => 3471411192, 389 | "6hJdfQ" => 3471083708, 390 | "6hRvdq" => 3472506540, 391 | "6hNpN9" => 3471902976, 392 | "6hMd75" => 3471668536, 393 | "6hLkks" => 3471497748, 394 | "6hHkYn" => 3470914553, 395 | "6hGZgY" => 3470844930, 396 | "6hGorv" => 3470727743, 397 | "6hG941" => 3470679342, 398 | "6hC6xK" => 3469890469, 399 | "6hTA5N" => 3472913142, 400 | "6hNzGE" => 3471936298, 401 | "6hN3F1" => 3471831918, 402 | "6hLdgN" => 3471473988, 403 | "6hFyvS" => 3470566524, 404 | "6hCxUM" => 3469979153, 405 | "6hCmje" => 3469940145, 406 | "6hC87z" => 3469895737, 407 | "6hC4mV" => 3469883113, 408 | "6hQXaJ" => 3472398736, 409 | "6hP6Tw" => 3472037848, 410 | "6hLx7r" => 3471537361, 411 | "6hFvYh" => 3470557964, 412 | "6hEPWM" => 3470423317, 413 | "6hDKDT" => 3470213769, 414 | "6hBrcn" => 3469761455, 415 | "6hBkh4" => 3469741543, 416 | "6hMdbq" => 3471668788, 417 | "6hK3cE" => 3471244996, 418 | "6hJSUj" => 3471213714, 419 | "6hJvFs" => 3471142324, 420 | "6hHVMu" => 3471028298, 421 | "6hHUZG" => 3471025642, 422 | "6hHeVT" => 3470894225, 423 | "6hFqjr" => 3470538949, 424 | "6hETNt" => 3470436291, 425 | "6hEPkX" => 3470421297, 426 | "6hCVrB" => 3470051585, 427 | "6hCE2V" => 3469999751, 428 | "6hNe3j" => 3471866794, 429 | "6hKjmA" => 3471299338, 430 | "6hHbMp" => 3470883641, 431 | "6hGWJ7" => 3470836354, 432 | "6hGn3F" => 3470723055, 433 | "6hFoKL" => 3470533690, 434 | "6hETWx" => 3470436759, 435 | "6hPaH5" => 3472050698, 436 | "6hNFFf" => 3471956400, 437 | "6hNnyp" => 3471895451, 438 | "6hM7ra" => 3471649459, 439 | "6hHof7" => 3470922194, 440 | "6hH8KM" => 3470873455, 441 | "6hGbCG" => 3470688024, 442 | "6hDXaB" => 3470252497, 443 | "6hDSF2" => 3470237383, 444 | "6hDfq8" => 3470115415, 445 | "6hCqqR" => 3469953985, 446 | "6hPpj7" => 3472096462, 447 | "6hMSHG" => 3471798434, 448 | "6hKyF6" => 3471347507, 449 | "6hK33v" => 3471244465, 450 | "6hJwva" => 3471145091, 451 | "6hGX9e" => 3470837753, 452 | "6hFWeR" => 3470639603, 453 | "6hD8oF" => 3470091783, 454 | "6hCopg" => 3469947165, 455 | "6hC9L8" => 3469901279, 456 | "6hBhS4" => 3469733423, 457 | "6hMQXy" => 3471792510, 458 | "6hLoRU" => 3471509606, 459 | "6hKCib" => 3471359692, 460 | "6hKder" => 3471278739, 461 | "6hHYPy" => 3471038510, 462 | "6hD4hz" => 3470077973, 463 | "6hPgTS" => 3472071508, 464 | "6hNXii" => 3472008951, 465 | "6hKYzE" => 3471427928, 466 | "6hKSr2" => 3471407243, 467 | "6hG4fB" => 3470663195, 468 | "6hFyz2" => 3470566707, 469 | "6hFoMT" => 3470533813, 470 | "6hC5V4" => 3469888341, 471 | "6hBZmZ" => 3469869661, 472 | "6hPXKU" => 3472205606, 473 | "6hHVwm" => 3471027420, 474 | "6hCzFH" => 3469985123, 475 | "6hCvsM" => 3469970917, 476 | "6hChKr" => 3469928151, 477 | "6hBv6F" => 3469774581, 478 | "6hPf62" => 3472065427, 479 | "6hNS4s" => 3471991328, 480 | "6hNPXe" => 3471984239, 481 | "6hLguh" => 3471484804, 482 | "6hJ5zB" => 3471057885, 483 | "6hHr6j" => 3470931776, 484 | "6hLBUx" => 3471553491, 485 | "6hLi1P" => 3471489939, 486 | "6hKQ35" => 3471399184, 487 | "6hKK67" => 3471382540, 488 | "6hHUcT" => 3471022985, 489 | "6hGPjm" => 3470811428, 490 | "6hF5ti" => 3470472183, 491 | "6hDZPz" => 3470261427, 492 | "6hC17D" => 3469872193, 493 | "6hR7R3" => 3472431292, 494 | "6hN7hS" => 3471844090, 495 | "6hHqoC" => 3470929416, 496 | "6hFDdx" => 3470582339, 497 | "6hFzdL" => 3470568896, 498 | "6hDuEH" => 3470163357, 499 | "6hCaZk" => 3469905409, 500 | "6hT2Hw" => 3472804260, 501 | "6hPhJY" => 3472074356, 502 | "6hKNBy" => 3471394398, 503 | "6hJPEq" => 3471202816, 504 | "6hGMH7" => 3470806020, 505 | "6hGp5L" => 3470729904, 506 | "6hFfRV" => 3470507135, 507 | "6hESHt" => 3470432637 508 | }, 509 | :bitcoin => { 510 | "6Hknds" => 3471391110, 511 | "6HeSMr" => 3470152229, 512 | "6Hiizc" => 3470988633, 513 | "6HikVM" => 3470993664, 514 | "6HmGgw" => 3471485480, 515 | "6Hcrkr" => 3469844075 516 | }, 517 | :ripple => { 518 | "aHk8d1" => 3471391110, 519 | "aHeSMi" => 3470152229, 520 | "aH55zc" => 3470988633, 521 | "aH5kVM" => 3470993664, 522 | "aHmGgA" => 3471485480, 523 | "aHciki" => 3469844075 524 | } 525 | } 526 | 527 | BINARY_STRING_EXAMPLES = { 528 | :flickr => { 529 | "6hKMCS" => "\xCE\xE99\x86".force_encoding('BINARY'), 530 | "6hDrmR" => "\xCE\xD6R%".force_encoding('BINARY'), 531 | "6hHHZB" => "\xCE\xE3\x15Y".force_encoding('BINARY'), 532 | "6hHKum" => "\xCE\xE3)\x00".force_encoding('BINARY'), 533 | "6hLgFW" => "\xCE\xEA\xAA(".force_encoding('BINARY'), 534 | "6hBRKR" => "\xCE\xD1\x9Ek".force_encoding('BINARY'), 535 | "7xBSG3j7NgPksBpPnX1G7y19EuAy4swQexDoECfWdys" => "\x01\xAD { 538 | "6Hknds" => "\xCE\xE99\x86".force_encoding('BINARY'), 539 | "6HeSMr" => "\xCE\xD6R%".force_encoding('BINARY'), 540 | "6Hiizc" => "\xCE\xE3\x15Y".force_encoding('BINARY'), 541 | "6HikVM" => "\xCE\xE3)\x00".force_encoding('BINARY'), 542 | "6HmGgw" => "\xCE\xEA\xAA(".force_encoding('BINARY'), 543 | "6Hcrkr" => "\xCE\xD1\x9Ek".force_encoding('BINARY'), 544 | "7Ycsh3K7oGpLTcQpNx1h7Z19fVbZ4TXqEYePfdFwDZT" => "\x01\xAD { 547 | "aHk8d1" => "\xCE\xE99\x86".force_encoding('BINARY'), 548 | "aHeSMi" => "\xCE\xD6R%".force_encoding('BINARY'), 549 | "aH55zc" => "\xCE\xE3\x15Y".force_encoding('BINARY'), 550 | "aH5kVM" => "\xCE\xE3)\x00".force_encoding('BINARY'), 551 | "aHmGgA" => "\xCE\xEA\xAA(".force_encoding('BINARY'), 552 | "aHciki" => "\xCE\xD1\x9Ek".force_encoding('BINARY'), 553 | "fYc16sKfoGFLTcQF4xr6fZr9CVbZhTXqNYePCdEADZT" => "\x01\xAD [ 559 | { 560 | :hex => "00000000000000000000123456789ABCDEF0", 561 | :b58 => "111111111143c9JGph3DZ", 562 | :b58_no_zeroes => "43c9JGph3DZ" 563 | }, 564 | { 565 | :hex => "00000000000000000000", 566 | :b58 => "1111111111", 567 | :b58_no_zeroes => "1" 568 | }, 569 | { 570 | :hex => "00", 571 | :b58 => "1", 572 | :b58_no_zeroes => "1" 573 | }, 574 | { 575 | :hex => "", 576 | :b58 => "1", 577 | :b58_no_zeroes => "1" 578 | }, 579 | ], 580 | :flickr => [ 581 | { 582 | :hex => "00000000000000000000123456789ABCDEF0", 583 | :b58 => "111111111143B9igPG3dy", 584 | :b58_no_zeroes => "43B9igPG3dy" 585 | }, 586 | { 587 | :hex => "00000000000000000000", 588 | :b58 => "1111111111", 589 | :b58_no_zeroes => "1" 590 | }, 591 | { 592 | :hex => "00", 593 | :b58 => "1", 594 | :b58_no_zeroes => "1" 595 | }, 596 | { 597 | :hex => "", 598 | :b58 => "1", 599 | :b58_no_zeroes => "1" 600 | }, 601 | ], 602 | :ripple => [ 603 | { 604 | :hex => "00000000000000000000123456789ABCDEF0", 605 | :b58 => "rrrrrrrrrrhsc9JGF6sDZ", 606 | :b58_no_zeroes => "hsc9JGF6sDZ" 607 | }, 608 | { 609 | :hex => "00000000000000000000", 610 | :b58 => "rrrrrrrrrr", 611 | :b58_no_zeroes => "r" 612 | }, 613 | { 614 | :hex => "00", 615 | :b58 => "r", 616 | :b58_no_zeroes => "r" 617 | }, 618 | { 619 | :hex => "", 620 | :b58 => "r", 621 | :b58_no_zeroes => "r" 622 | } 623 | ] 624 | } 625 | 626 | HEX_TO_BIN = ->hex { [hex].pack('H*') } 627 | HEX_TO_INT = ->hex { hex.to_i(16) } 628 | 629 | STRIP_LEADING = ->char { ->str { str.gsub(/^#{char}*/, '') } } 630 | STRIP_BINARY = STRIP_LEADING["\x00"] 631 | 632 | LEADING_ZEROES_EXAMPLES.each do |alphabet, examples| 633 | examples.each do |example| 634 | define_method("test_binary_to_base58_leading_zeroes_#{alphabet}_#{example[:hex]}") do 635 | bin = HEX_TO_BIN[example[:hex]] 636 | bin_no_zeroes = STRIP_BINARY[bin] 637 | 638 | assert_equal example[:b58], Base58.binary_to_base58(bin, alphabet) # Default behavior is to include leading zeroes 639 | assert_equal example[:b58], Base58.binary_to_base58(bin, alphabet, true) 640 | assert_equal example[:b58_no_zeroes], Base58.binary_to_base58(bin, alphabet, false) # Optionally, leading zeroes may be omitted 641 | 642 | # If there are no zeroes in the input, there are never zeroes in the output 643 | assert_equal example[:b58_no_zeroes], Base58.binary_to_base58(bin_no_zeroes, alphabet) 644 | assert_equal example[:b58_no_zeroes], Base58.binary_to_base58(bin_no_zeroes, alphabet, true) 645 | assert_equal example[:b58_no_zeroes], Base58.binary_to_base58(bin_no_zeroes, alphabet, false) 646 | end 647 | end 648 | end 649 | 650 | LEADING_ZEROES_EXAMPLES.each do |alphabet, examples| 651 | examples.each do |example| 652 | define_method("test_base58_to_binary_leading_zeroes_#{alphabet}_#{example[:hex]}") do 653 | bin = HEX_TO_BIN[example[:hex]] 654 | bin_no_zeroes = STRIP_BINARY[bin] 655 | 656 | # When converting from base58, an empty string will never be produced. 657 | bin_no_zeroes = "\x00" if bin_no_zeroes.empty? 658 | bin = "\x00" if bin.empty? 659 | 660 | assert_equal bin, Base58.base58_to_binary(example[:b58], alphabet) 661 | assert_equal bin_no_zeroes, Base58.base58_to_binary(example[:b58_no_zeroes], alphabet) 662 | end 663 | end 664 | end 665 | 666 | LEADING_ZEROES_EXAMPLES.each do |alphabet, examples| 667 | examples.each do |example| 668 | define_method("test_int_to_base58_leading_zeroes_#{alphabet}_#{example[:hex]}") do 669 | assert_equal example[:b58_no_zeroes], Base58.int_to_base58(HEX_TO_INT[example[:hex]], alphabet) 670 | end 671 | end 672 | end 673 | 674 | LEADING_ZEROES_EXAMPLES.each do |alphabet, examples| 675 | examples.each do |example| 676 | define_method("test_base58_to_int_leading_zeroes_#{alphabet}_#{example[:hex]}") do 677 | assert_equal HEX_TO_INT[example[:hex]], Base58.base58_to_int(example[:b58], alphabet) 678 | assert_equal HEX_TO_INT[example[:hex]], Base58.base58_to_int(example[:b58_no_zeroes], alphabet) 679 | end 680 | end 681 | end 682 | 683 | def test_base58_to_binary_empty_input 684 | assert_equal "\x00", Base58.base58_to_binary("") 685 | end 686 | 687 | def test_base58_to_int_leading_zeroes_other_examples 688 | # All other examples, when prepended with varying amounts of leading zeroes, should work properly. 689 | EXAMPLES.each do |alphabet, examples| 690 | examples.each do |base_58, expected| 691 | (0..50).each do |n| 692 | assert_equal expected, Base58.base58_to_int( (Base58::ALPHABETS[alphabet][0] * n) + base_58, alphabet ) 693 | end 694 | end 695 | end 696 | end 697 | 698 | def test_binary_to_base58_all_alphabets 699 | BINARY_STRING_EXAMPLES.each do |alphabet, examples| 700 | examples.each do |expected, integer| 701 | assert_equal expected, Base58.binary_to_base58(integer, alphabet) 702 | end 703 | end 704 | end 705 | 706 | def test_base58_to_binary_all_alphabets 707 | BINARY_STRING_EXAMPLES.each do |alphabet, examples| 708 | examples.each do |base_58, expected| 709 | assert_equal expected, Base58.base58_to_binary(base_58, alphabet) 710 | end 711 | end 712 | end 713 | 714 | def test_base58_to_binary_round_trip 715 | BINARY_STRING_EXAMPLES.each do |alphabet, examples| 716 | examples.each do |base_58, binary| 717 | assert_equal base_58, Base58.binary_to_base58(Base58.base58_to_binary(base_58, alphabet), alphabet) 718 | assert_equal binary, Base58.base58_to_binary(Base58.binary_to_base58(binary, alphabet), alphabet) 719 | end 720 | end 721 | end 722 | 723 | def test_int_to_base58_all_alphabets 724 | EXAMPLES.each do |alphabet, examples| 725 | examples.each do |expected, integer| 726 | assert_equal expected, Base58.int_to_base58(integer, alphabet) 727 | end 728 | end 729 | end 730 | 731 | def test_base58_to_int_all_alphabets 732 | EXAMPLES.each do |alphabet, examples| 733 | examples.each do |base_58, expected| 734 | assert_equal expected, Base58.base58_to_int(base_58, alphabet) 735 | end 736 | end 737 | end 738 | 739 | def test_int_to_base58_default_alphabet 740 | EXAMPLES[:flickr].each do |expected, integer| 741 | assert_equal expected, Base58.int_to_base58(integer) 742 | end 743 | end 744 | 745 | def test_base58_to_int_default_alphabet 746 | EXAMPLES[:flickr].each do |base_58, expected| 747 | assert_equal expected, Base58.base58_to_int(base_58) 748 | end 749 | end 750 | 751 | def test_string_instead_of_int 752 | e = assert_raise(ArgumentError) { Base58.int_to_base58('3470432637') } 753 | assert_equal 'Value passed is not an Integer.', e.message 754 | end 755 | 756 | def test_invalid_base58_string 757 | e = assert_raise(ArgumentError) { Base58.base58_to_int('nh?') } 758 | assert_equal 'Value passed not a valid Base58 String.', e.message 759 | end 760 | 761 | def test_encode_decode_aliases 762 | int = 3471844090 763 | str = Base58.encode(int) 764 | assert_equal str, "6hN7hS" 765 | assert_equal int, Base58.decode(str) 766 | end 767 | 768 | end --------------------------------------------------------------------------------