Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A Crystal shard for base encoding / decoding of any given alphabet with optional bitcoin-style leading zero compression.

License

NotificationsYou must be signed in to change notification settings

wout/base_x

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Crystal shard for base encoding/decoding of any given alphabet with optionalbitcoin-style leading zero compression.

GitHubGitHub tag (latest SemVer)GitHub Workflow Status

A note on performance

Because this library also decodes to integers, it relies onBigInt, whichimplies a significant performance impact. If you are looking to decode/encodebase58 and you don't need to decode to integers,wyhaines/base58.cr is a betteralternative as it's a lot faster.

Supported alphabets

BaseAlphabet
201
801234567
110123456789a
26ABCDEFGHIJKLMNOPQRSTUVWXYZ
26abcdefghijklmnopqrstuvwxyz (lowercase)
32ybndrfg8ejkmcpqxot1uwisza345h769 (z-base-32)
360123456789abcdefghijklmnopqrstuvwxyz
450123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:
52ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
58123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz (bitcoin)
58123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ (flickr)
58rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz (ripple)
620123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
67ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.!~

Installation

  1. Add the dependency to yourshard.yml:
dependencies:base_x:github:wout/base_x
  1. Runshards install

Usage

require"base_x"

Converting bytes

BaseX::Base58.decode("6hKMCS")# => Bytes[206, 233, 57, 134]BaseX::Base58.encode(Bytes[206,233,57,134])# => "6hKMCS"

or:

BaseX::Base45.decode("5R%/+F5SZ6LLW+J60D")# => Bytes[231, 5, 88, 240, 215, 89, 21, 180, 87, 179, 227, 86]BaseX::Base45.encode(Bytes[231,5,88,240,215,89,21,180,87,179,227,86])# => "5R%/+F5SZ6LLW+J60D"

Note:BaseX::BaseXX.decode is the same asBaseX::BaseXX.decode_bytes.

Converting integers

BaseX::Base58.decode_int("6hKMCS")# => 3471391110BaseX::Base58.encode(3471391110)# => "6hKMCS"

Using other alphabets

base_16_alphabet="0123456789abcdef"BaseX.decode("ff3300", base_16_alphabet)# => Bytes[255, 51, 0]BaseX.encode(Bytes[255,51,0], base_16_alphabet)# => "ff3300"

Leading zeroes

Some protocols, such as Bitcoin, require leading zeros to be encoded. Passingtrue to the third argument ofBaseX::Base58.encode will enable thisbehaviour.

bitcoin_address_hex="00000000000000000000123456789ABCDEF0"bitcoin_address_bytes=String.new(bitcoin_address_hex.hexbytes)BaseX::Base58.encode(bitcoin_address_bytes,leading_zeroes:true)# => 111111111143c9JGph3DZ

Development

Make sure you haveGuardian.cr installed. Thenrun:

$ guardian

This will automatically:

  • run ameba for src and spec files
  • run the relevant spec for any file in the src dir
  • run spec a file whenever it's saved

Contributing

  1. Fork it (https://github.com/wout/base_x/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • Wout - creator and maintainer

Acknowledgments

This shard pulls inspiration from the following projects:

About

A Crystal shard for base encoding / decoding of any given alphabet with optional bitcoin-style leading zero compression.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp