lua-basex

使用比特币风格的领先零压缩对任何给定字母表进行编码/解码

$ opm get un-def/lua-basex

lua-basex

使用比特币风格的领先零压缩对任何给定字母表进行编码/解码。这是对 base-x 的 Lua 实现。

安装

    $ luarocks install lua-basex

或者

    $ opm get un-def/lua-basex

依赖

lua-basex 没有依赖关系(当然,除了 Lua 本身)。

兼容性

已在 Lua 5.1、5.2 和 5.3 上测试。

示例

    local basex = require('basex')
    
    local encoded, decoded
    local base58alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
    
    local base58 = basex(base58alphabet)
    decoded = base58:decode('5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr')
    print(decoded)
    --> [string of bytes]
    encoded = base58:encode(decoded)
    print(encoded)
    --> 5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr
    
    local base2 = basex('01')
    encoded = base2:encode('\0\255')
    print(encoded)
    --> 011111111
    decoded = base2:decode(encoded)
    print(decoded:byte(1, 2))
    --> 0      255

有很多预定义的编码器/解码器

  • base16lower

  • base16upper

  • base58bitcoin

  • base58flickr

  • base58ripple

因此,第一个示例可以改写如下

    local basex = require('basex')
    
    local decoded = basex.base58bitcoin:decode('5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr')
    local encoded = basex.base58bitcoin:encode(decoded)

任何 basex.baseXvariant 预定义编码器/解码器的字母表都可以作为 basex.alphabets.BASEXVARIANT 获得(例如,basex.alphabets.BASE58BITCOIN)。

作者

un.def

许可证

2bsd

版本

  • 使用比特币风格的领先零压缩对任何给定字母表进行编码/解码 2017-06-29 18:28:56
  • 使用比特币风格的领先零压缩对任何给定字母表进行编码/解码 2017-06-28 18:51:10