pax_global_header00006660000000000000000000000064127032473550014522gustar00rootroot0000000000000052 comment=8e88b6de3e28222bddb71ba494551fedbc1a32be basexx-0.3.0/000077500000000000000000000000001270324735500130145ustar00rootroot00000000000000basexx-0.3.0/.busted000066400000000000000000000001501270324735500142770ustar00rootroot00000000000000return { default = { ROOT = { "test" }, verbose = true, lpath = "lib/?.lua" } } basexx-0.3.0/.travis.yml000066400000000000000000000006671270324735500151360ustar00rootroot00000000000000language: python sudo: false env: matrix: - LUA="lua=5.1" - LUA="lua=5.2" - LUA="lua=5.3" - LUA="luajit=2.0" - LUA="luajit=2.1" branches: only: - master before_install: - pip install hererocks - hererocks base -r^ --$LUA - export PATH=$PATH:$PWD/base/bin - luarocks install busted script: - busted -v notifications: email: on_success: change on_failure: always basexx-0.3.0/CHANGELOG.adoc000066400000000000000000000013761270324735500151420ustar00rootroot00000000000000= changelog == from 0.2.0 to 0.3.0 * fix: updated outdated README example for crockford * added the examples as tests for the crockford examples * improved from functions ** added the possibility to ignore characters in the encoded string ** handles now unknown characters in the encoded string without a crash == from 0.1.1 to 0.2.0 * fix: supports now for unit tests busted 2.0 * added the functions from_url64 / to_url64 and from_z85 / to_z85 * removed the mapping for U and u in from crockford * internal: ** travis.ci support ** 80 lines width ** busted 2.0 support ** switched from MarkDown to AsciiDoc == from 0.1.0 to 0.1.1 * fix: divider_string & number_to_bit should be local‎ * better README * internal: divided the unit tests into separate files basexx-0.3.0/LICENSE000066400000000000000000000020561270324735500140240ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2013 aiq Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. basexx-0.3.0/README.adoc000066400000000000000000000063701270324735500146070ustar00rootroot00000000000000= basexx image::https://api.travis-ci.org/aiq/basexx.png[travis] A Lua library for base2, base16, base32, base64, base85 decoding and encoding of data strings. == API For every supported format has basexx a *_from_* and *_to_* function. The *_from_* functions have two parameters: * *str* which represent the encoded string that should be decoded * *ignore* is an optional set of characters that should be ignored in the string, see the *_from_bit_* and *_from_hex_* examples The *_from_* functions return a string if the string can be decoded, otherwise *_nil_* and information which character caused the error. The *_to_* functions have just one parameter: * *str* the data string that should be encoded === from_bit / to_bit Converts a byte string to a bitfield string. * 0, O and o maps to the same value * 1, I, i, L and l maps to the same value [source,lua] ---- basexx.to_bit( "ACDC" ) --> 01000001010000110100010001000011 basexx.from_bit( "01000001010000110100010001000011" ) --> ACDC basexx.from_bit( "o1ooooo1o1oooo11" ) --> AC basexx.from_bit( "Oioooooi Oiooooii\n", " \n" ) --> AC ---- === from_hex / to_hex Converts a byte string to a uppercase http://tools.ietf.org/html/rfc3548#section-6[hex] data string. [source,lua] ---- basexx.to_hex( "Hello world!" ) --> 48656C6C6F20776F726C6421 basexx.from_hex( "4865-6C6C 6F20-776F 726C-6421", "- " ) --> Hello world! basexx.from_hex( "48656c6c6f20776f726c6421" ) --> Hello world! ---- === from_base32 / to_base32 Converts a byte string to a http://tools.ietf.org/html/rfc3548#section-5[base32(_rfc3548)] uppercase data string. * It's case insensitive [source,lua] ---- basexx.to_base32( "chunky bacon!" ) --> MNUHK3TLPEQGEYLDN5XCC=== basexx.from_base32( "MNUHK3TLPEQGEYLDN5XCC===" ) --> chunky bacon! ---- === from_crockford / to_crockford Converts a byte string to a http://www.crockford.com/wrmg/base32.html[base32(_crockford_)] uppercase data string. The optional check value is not implemented. * It's case insensitive * 1, I, i, L and l maps to the same value * 0, O and o maps to the same value [source,lua] ---- string.lower( basexx.to_crockford( "Hello World" ) ) --> 91jprv3f41bpywkccg basexx.from_crockford( "axqqeb10d5t20wk5c5p6ry90exqq4tvk44" ) --> Wow, it really works! ---- === from_base64 / to_base64 Converts a byte string to a https://tools.ietf.org/html/rfc4648#section-4[base64] data string. [source,lua] ---- basexx.to_base64( "Man") --> TWFu basexx.from_base64( "TWFu" ) --> Man ---- === from_url64 / to_url64 Same as above, but uses a https://tools.ietf.org/html/rfc4648#section-5[URL Safe base64] alphabet and no padding. === from_z85 / to_z85 Converts a byte string to a http://rfc.zeromq.org/spec:32[base85(ZeroMQ)] data string. to_z85 expects only a binary string that length is divisible by 4 with no remainder, and from_z85 expects only printable a string that length is divisible by 5 with no remainder. [source,lua] ---- basexx.to_z85( "1234" ) --> f!$Kw basexx.from_z85( "f!$Kw" ) --> 1234 ---- == Installation To install the version 0.3.0 of basexx use LuaRocks with the following line. ---- luarocks install basexx ---- If you want to use the current development version, clone this repository and use LuaRocks with the following command. ---- luarocks make dist/basexx-scm-0.rockspec ---- basexx-0.3.0/dist/000077500000000000000000000000001270324735500137575ustar00rootroot00000000000000basexx-0.3.0/dist/basexx-0.1.0-1.rockspec000066400000000000000000000011311270324735500175700ustar00rootroot00000000000000package = "basexx" version = "0.1.0-1" description = { summary = "A base2, base32 and base64 library for Lua", detailed = "A library which provides base2(bitfield), base32(crock ford/rfc 3548), base64 decoding and encoding.", license = "MIT", homepage = "https://github.com/aiq/basexx" } dependencies = { "lua >= 5.1" } source = { url = "https://github.com/aiq/basexx/archive/v0.1.0.tar.gz", md5 = "66570a1e354ce0c919192c895a1ee8bb", dir = "basexx-0.1.0" } build = { type = 'builtin', modules = { basexx = "lib/basexx.lua" }, copy_directories = { "test" } }basexx-0.3.0/dist/basexx-0.1.1-1.rockspec000066400000000000000000000011311270324735500175710ustar00rootroot00000000000000package = "basexx" version = "0.1.1-1" description = { summary = "A base2, base32 and base64 library for Lua", detailed = "A library which provides base2(bitfield), base32(crock ford/rfc 3548), base64 decoding and encoding.", license = "MIT", homepage = "https://github.com/aiq/basexx" } dependencies = { "lua >= 5.1" } source = { url = "https://github.com/aiq/basexx/archive/v0.1.1.tar.gz", md5 = "6481b5f980c0da1248821273271290be", dir = "basexx-0.1.1" } build = { type = 'builtin', modules = { basexx = "lib/basexx.lua" }, copy_directories = { "test" } }basexx-0.3.0/dist/basexx-0.2.0-1.rockspec000066400000000000000000000012171270324735500175760ustar00rootroot00000000000000package = "basexx" version = "0.2.0-1" description = { summary = "A base2, base16, base32, base64 and base85 library for Lua", detailed = "A Lua library which provides base2(bitfield), base16(hex), base32(crockford/rfc), base64(rfc/url), base85(z85) decoding and encoding.", license = "MIT", homepage = "https://github.com/aiq/basexx" } source = { url = "https://github.com/aiq/basexx/archive/v0.2.0.tar.gz", md5 = "2c40dd08dbd39310d312756f8eda178b", dir = "basexx-0.2.0" } dependencies = { "lua >= 5.1" } build = { type = 'builtin', modules = { basexx = "lib/basexx.lua" }, copy_directories = { "test" } } basexx-0.3.0/dist/basexx-scm-0.rockspec000066400000000000000000000010271270324735500177210ustar00rootroot00000000000000package = "basexx" version = "scm-0" description = { summary = "A base2, base16, base32, base64 and base85 library for Lua", detailed = "A Lua library which provides base2(bitfield), base16(hex), base32(crockford/rfc), base64(rfc/url), base85(z85) decoding and encoding.", license = "MIT", homepage = "https://github.com/aiq/basexx" } source = { url = "..." } dependencies = { "lua >= 5.1" } build = { type = 'builtin', modules = { basexx = "lib/basexx.lua" }, copy_directories = { "test" } } basexx-0.3.0/lib/000077500000000000000000000000001270324735500135625ustar00rootroot00000000000000basexx-0.3.0/lib/basexx.lua000066400000000000000000000216011270324735500155570ustar00rootroot00000000000000-------------------------------------------------------------------------------- -- util functions -------------------------------------------------------------------------------- local function divide_string( str, max, fillChar ) fillChar = fillChar or "" local result = {} local start = 1 for i = 1, #str do if i % max == 0 then table.insert( result, str:sub( start, i ) ) start = i + 1 elseif i == #str then table.insert( result, str:sub( start, i ) ) end end return result end local function number_to_bit( num, length ) local bits = {} while num > 0 do local rest = math.floor( math.fmod( num, 2 ) ) table.insert( bits, rest ) num = ( num - rest ) / 2 end while #bits < length do table.insert( bits, "0" ) end return string.reverse( table.concat( bits ) ) end local function ignore_set( str, set ) if set then str = str:gsub( "["..set.."]", "" ) end return str end local function pure_from_bit( str ) return ( str:gsub( '........', function ( cc ) return string.char( tonumber( cc, 2 ) ) end ) ) end -------------------------------------------------------------------------------- local basexx = {} -------------------------------------------------------------------------------- -- base2(bitfield) decode and encode function -------------------------------------------------------------------------------- local bitMap = { o = "0", i = "1", l = "1" } function basexx.from_bit( str, ignore ) str = ignore_set( str, ignore ) str = string.lower( str ) str = str:gsub( '[ilo]', function( c ) return bitMap[ c ] end ) local wrong = str:match( "[^01]" ) if wrong then return nil, wrong end return pure_from_bit( str ) end function basexx.to_bit( str ) return ( str:gsub( '.', function ( c ) local byte = string.byte( c ) local bits = {} for i = 1,8 do table.insert( bits, byte % 2 ) byte = math.floor( byte / 2 ) end return table.concat( bits ):reverse() end ) ) end -------------------------------------------------------------------------------- -- base16(hex) decode and encode function -------------------------------------------------------------------------------- function basexx.from_hex( str, ignore ) str = ignore_set( str, ignore ) local wrong = str:match( "[^%x]" ) if wrong then return nil, wrong end return ( str:gsub( '..', function ( cc ) return string.char( tonumber( cc, 16 ) ) end ) ) end function basexx.to_hex( str ) return ( str:gsub( '.', function ( c ) return string.format('%02X', string.byte( c ) ) end ) ) end -------------------------------------------------------------------------------- -- generic function to decode and encode base32/base64 -------------------------------------------------------------------------------- local function from_basexx( str, alphabet, bits ) local result = {} for i = 1, #str do local c = string.sub( str, i, i ) if c ~= '=' then local index = string.find( alphabet, c, 1, true ) if not index or c == "$" then return nil, c end table.insert( result, number_to_bit( index - 1, bits ) ) end end local value = table.concat( result ) local pad = #value % 8 return pure_from_bit( string.sub( value, 1, #value - pad ) ) end local function to_basexx( str, alphabet, bits, pad ) local bitString = basexx.to_bit( str ) local chunks = divide_string( bitString, bits ) local result = {} for key,value in ipairs( chunks ) do if ( #value < bits ) then value = value .. string.rep( '0', bits - #value ) end local pos = tonumber( value, 2 ) + 1 table.insert( result, alphabet:sub( pos, pos ) ) end table.insert( result, pad ) return table.concat( result ) end -------------------------------------------------------------------------------- -- rfc 3548: http://www.rfc-editor.org/rfc/rfc3548.txt -------------------------------------------------------------------------------- local base32Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" local base32PadMap = { "", "======", "====", "===", "=" } function basexx.from_base32( str, ignore ) str = ignore_set( str, ignore ) return from_basexx( string.upper( str ), base32Alphabet, 5 ) end function basexx.to_base32( str ) return to_basexx( str, base32Alphabet, 5, base32PadMap[ #str % 5 + 1 ] ) end -------------------------------------------------------------------------------- -- crockford: http://www.crockford.com/wrmg/base32.html -------------------------------------------------------------------------------- local crockfordAlphabet = "0123456789ABCDEFGHJKMNPQRSTVWXYZ" local crockfordMap = { O = "0", I = "1", L = "1" } function basexx.from_crockford( str, ignore ) str = ignore_set( str, ignore ) str = string.upper( str ) str = str:gsub( '[ILOU]', function( c ) return crockfordMap[ c ] end ) return from_basexx( str, crockfordAlphabet, 5 ) end function basexx.to_crockford( str ) return to_basexx( str, crockfordAlphabet, 5, "" ) end -------------------------------------------------------------------------------- -- base64 decode and encode function -------------------------------------------------------------------------------- local base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".. "abcdefghijklmnopqrstuvwxyz".. "0123456789+/" local base64PadMap = { "", "==", "=" } function basexx.from_base64( str, ignore ) str = ignore_set( str, ignore ) return from_basexx( str, base64Alphabet, 6 ) end function basexx.to_base64( str ) return to_basexx( str, base64Alphabet, 6, base64PadMap[ #str % 3 + 1 ] ) end -------------------------------------------------------------------------------- -- URL safe base64 decode and encode function -------------------------------------------------------------------------------- local url64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".. "abcdefghijklmnopqrstuvwxyz".. "0123456789-_" function basexx.from_url64( str, ignore ) str = ignore_set( str, ignore ) return from_basexx( str, url64Alphabet, 6 ) end function basexx.to_url64( str ) return to_basexx( str, url64Alphabet, 6, "" ) end -------------------------------------------------------------------------------- -- -------------------------------------------------------------------------------- local z85Decoder = { 0x00, 0x44, 0x00, 0x54, 0x53, 0x52, 0x48, 0x00, 0x4B, 0x4C, 0x46, 0x41, 0x00, 0x3F, 0x3E, 0x45, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x40, 0x00, 0x49, 0x42, 0x4A, 0x47, 0x51, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x4D, 0x00, 0x4E, 0x43, 0x00, 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00 } function basexx.from_z85( str, ignore ) str = ignore_set( str, ignore ) if ( #str % 5 ) ~= 0 then return nil, #str % 5 end local result = {} local value = 0 for i = 1, #str do local index = string.byte( str, i ) - 31 if index < 1 or index >= #z85Decoder then return nil, index end value = ( value * 85 ) + z85Decoder[ index ] if ( i % 5 ) == 0 then local divisor = 256 * 256 * 256 while divisor ~= 0 do local b = math.floor( value / divisor ) % 256 table.insert( result, string.char( b ) ) divisor = math.floor( divisor / 256 ) end value = 0 end end return table.concat( result ) end local z85Encoder = "0123456789".. "abcdefghijklmnopqrstuvwxyz".. "ABCDEFGHIJKLMNOPQRSTUVWXYZ".. ".-:+=^!/*?&<>()[]{}@%$#" function basexx.to_z85( str ) if ( #str % 4 ) ~= 0 then return nil, #str, 4 end local result = {} local value = 0 for i = 1, #str do local b = string.byte( str, i ) value = ( value * 256 ) + b if ( i % 4 ) == 0 then local divisor = 85 * 85 * 85 * 85 while divisor ~= 0 do local index = ( math.floor( value / divisor ) % 85 ) + 1 table.insert( result, z85Encoder:sub( index, index ) ) divisor = math.floor( divisor / 85 ) end value = 0 end end return table.concat( result ) end -------------------------------------------------------------------------------- return basexx basexx-0.3.0/test/000077500000000000000000000000001270324735500137735ustar00rootroot00000000000000basexx-0.3.0/test/base32_spec.lua000066400000000000000000000014531270324735500165720ustar00rootroot00000000000000basexx = require( "basexx" ) describe( "should handle base32(rfc3548) strings", function() it( "should convert chunky bacon", function() -- https://github.com/stesla/base32 assert.is.same( "MNUHK3TLPEQGEYLDN5XCC===", basexx.to_base32( "chunky bacon!" ) ) assert.is.same( "chunky bacon!", basexx.from_base32( "MNUHK3TLPEQGEYLDN5XCC===" ) ) end) it( "should allow to ignore characters in a base32 string", function() assert.is.same( "chunky bacon!", basexx.from_base32( "MNUHK3TLPEQGEYLDN5XCC===" ) ) end) it ( "should handle wrong characters without a crash", function() local res, err = basexx.from_base32( "MS$DF" ) assert.is.falsy( res ) assert.is.same( "$", err ) end) end) basexx-0.3.0/test/base64_spec.lua000066400000000000000000000063461270324735500166050ustar00rootroot00000000000000basexx = require( "basexx" ) describe( "should handle base64 strings", function() local longtxt = "Man is distinguished, not only by his reason, but by ".. "this singular passion from other animals, which is a ".. "lust of the mind, that by a perseverance of delight in ".. "the continued and indefatigable generation of knowledge, ".. "exceeds the short vehemence of any carnal pleasure." local long64 = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb2".. "4sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBh".. "bmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYn".. "kgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVk".. "IGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLC".. "BleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBw".. "bGVhc3VyZS4=" it( "should work with wikipedia examples", function() -- http://en.wikipedia.org/wiki/Base64 assert.is.same( 'TWFu', basexx.to_base64( 'Man') ) assert.is.same( 'Man', basexx.from_base64( 'TWFu') ) assert.is.same( 'bGVhc3VyZS4=', basexx.to_base64( 'leasure.') ) assert.is.same( 'leasure.', basexx.from_base64( 'bGVhc3VyZS4=') ) assert.is.same( 'cGxlYXN1cmUu', basexx.to_base64( 'pleasure.') ) assert.is.same( 'pleasure.', basexx.from_base64( 'cGxlYXN1cmUu') ) assert.is.same( 'ZWFzdXJlLg==', basexx.to_base64( 'easure.') ) assert.is.same( 'easure.', basexx.from_base64( 'ZWFzdXJlLg==') ) assert.is.same( 'c3VyZS4=', basexx.to_base64( 'sure.') ) assert.is.same( 'sure.', basexx.from_base64( 'c3VyZS4=') ) assert.is.same( long64, basexx.to_base64( longtxt ) ) assert.is.same( longtxt, basexx.from_base64( long64) ) end) it( "should handle padding correct", function() -- http://en.wikipedia.org/wiki/Base64#Padding assert.is.same( "YW55IGNhcm5hbCBwbGVhc3VyZS4=", basexx.to_base64( "any carnal pleasure." ) ) assert.is.same( "YW55IGNhcm5hbCBwbGVhc3VyZQ==", basexx.to_base64( "any carnal pleasure" ) ) assert.is.same( "YW55IGNhcm5hbCBwbGVhc3Vy", basexx.to_base64( "any carnal pleasur" ) ) assert.is.same( "YW55IGNhcm5hbCBwbGVhc3U=", basexx.to_base64( "any carnal pleasu" ) ) assert.is.same( "YW55IGNhcm5hbCBwbGVhcw==", basexx.to_base64( "any carnal pleas" ) ) assert.is.same( "any carnal pleas", basexx.from_base64( "YW55IGNhcm5hbCBwbGVhcw==" ) ) assert.is.same( "any carnal pleasu", basexx.from_base64( "YW55IGNhcm5hbCBwbGVhc3U=" ) ) assert.is.same( "any carnal pleasur", basexx.from_base64( "YW55IGNhcm5hbCBwbGVhc3Vy" ) ) end) it( "should allow to ignore characters in a base64 string", function() assert.is.same( "Man", basexx.from_base64( "TW-Fu", "-" ) ) end) it( "should handle wrong characters without a crash", function() local res, err = basexx.from_base64( "TW`Fu" ) assert.is.falsy( res ) assert.is.same( "`", err ) end) end) basexx-0.3.0/test/bit_spec.lua000066400000000000000000000020001270324735500162560ustar00rootroot00000000000000basexx = require( "basexx" ) describe( "should handle bitfields strings", function() it( "should convert data to a bitfields string", function() assert.is.same( "01000001010000110100010001000011", basexx.to_bit( "ACDC" ) ) end) it( "should read data from a bitfields string", function() assert.is.same( "ACDC", basexx.from_bit( "01000001010000110100010001000011" ) ) end) it( "should read data with o instead of 0", function() assert.is.same( "AC", basexx.from_bit( "o1ooooo1o1oooo11" ) ) assert.is.same( "AC", basexx.from_bit( "OioooooiOiooooii" ) ) end) it( "should allow to ignore characters in a bitfield string", function() assert.is.same( "AC", basexx.from_bit( "o1ooooo1 o1oooo11", " " ) ) end) it( "should handle wrong characters without a crash", function() local res, err = basexx.from_bit( "o1oo*ooo1*o1oo*oo11" ) assert.is.falsy( res ) assert.is.same( "*", err ) end) end) basexx-0.3.0/test/crockford_spec.lua000066400000000000000000000022071270324735500174650ustar00rootroot00000000000000basexx = require( "basexx" ) describe( "should handle base32(crockford) strings", function() it( "should fulfill crockford-py test case", function() -- https://github.com/ingydotnet/crockford-py/blob/master/tests/test_functions.py assert.is.same( "CSQPY", basexx.to_crockford( "foo" ) ) assert.is.same( "foo", basexx.from_crockford( "CSQPY" ) ) end) it( "should really work ;-)", function() -- https://github.com/aiq/basexx/issues/3 assert.is.same( "91jprv3f41bpywkccg", string.lower( basexx.to_crockford( "Hello World" ) ) ) assert.is.same( "AXQQEB10D5T20WK5C5P6RY90EXQQ4TVK44", basexx.to_crockford( "Wow, it really works!" ) ) assert.is.same( "Wow, it really works!", basexx.from_crockford( "axqqeb10d5t20wk5c5p6ry90exqq4tvk44" ) ) end) it( "should allow to ignore characters in a crockford string", function() assert.is.same( "foo", basexx.from_crockford( "CSQPY\n", "\n" ) ) end) it( "should allow to ignore characters without a crash", function() local res, err = basexx.from_crockford( "CSQ%PY" ) assert.is.falsy( res ) assert.is.same( "%", err ) end) end) basexx-0.3.0/test/hex_spec.lua000066400000000000000000000016631270324735500163020ustar00rootroot00000000000000basexx = require( "basexx" ) describe( "should handle hex strings", function() it( "should convert data to a hex string", function() assert.is.same( "48656C6C6F20776F726C6421", basexx.to_hex( "Hello world!" ) ) end) it( "should read data from a upper and lower hex string", function() assert.is.same( "Hello world!", basexx.from_hex( "48656C6C6F20776F726C6421" ) ) assert.is.same( "Hello world!", basexx.from_hex( "48656c6c6f20776f726c6421" ) ) end) it( "should allow to ignore characters in a hex string", function() assert.is.same( "Hello world!", basexx.from_hex( "4865-6c6c 6f20-776f 726c-6421", " -" ) ) end) it( "should handle wrong characters without a crash", function() local res, err = basexx.from_hex( "4865-6c6c" ) assert.is.falsy( res ) assert.is.same( "-", err ) end) end) basexx-0.3.0/test/url64_spec.lua000066400000000000000000000052661270324735500164750ustar00rootroot00000000000000basexx = require( "basexx" ) describe( "should handle base64 strings", function() local longtxt = "Man is distinguished, not only by his reason, but by ".. "this singular passion from other animals, which is a ".. "lust of the mind, that by a perseverance of delight in ".. "the continued and indefatigable generation of knowledge, ".. "exceeds the short vehemence of any carnal pleasure." local long64 = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb2".. "4sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBh".. "bmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYn".. "kgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVk".. "IGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLC".. "BleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBw".. "bGVhc3VyZS4" it( "should convert data to a base64 string", function() -- http://en.wikipedia.org/wiki/Base64#URL_applications assert.is.same( 'TWFu', basexx.to_url64( 'Man') ) assert.is.same( 'Man', basexx.from_url64( 'TWFu') ) assert.is.same( 'bGVhc3VyZS4', basexx.to_url64( 'leasure.') ) assert.is.same( 'leasure.', basexx.from_url64( 'bGVhc3VyZS4') ) assert.is.same( 'cGxlYXN1cmUu', basexx.to_url64( 'pleasure.') ) assert.is.same( 'pleasure.', basexx.from_url64( 'cGxlYXN1cmUu') ) assert.is.same( 'ZWFzdXJlLg', basexx.to_url64( 'easure.') ) assert.is.same( 'easure.', basexx.from_url64( 'ZWFzdXJlLg') ) assert.is.same( 'c3VyZS4', basexx.to_url64( 'sure.') ) assert.is.same( 'sure.', basexx.from_url64( 'c3VyZS4') ) assert.is.same( long64, basexx.to_url64( longtxt ) ) assert.is.same( longtxt, basexx.from_url64( long64 ) ) end) local msgtxt = '{"msg_en":"Hello","msg_jp":"こんにちは","msg_cn":"你好"'.. ',"msg_kr":"안녕하세요","msg_ru":"Здравствуйте!"'.. ',"msg_de":"Grüß Gott"}' local msg64 = 'eyJtc2dfZW4iOiJIZWxsbyIsIm1zZ19qcCI6IuOBk-OCk-OBq-OBoeOBryI'.. 'sIm1zZ19jbiI6IuS9oOWlvSIsIm1zZ19rciI6IuyViOuFle2VmOyEuOyalC'.. 'IsIm1zZ19ydSI6ItCX0LTRgNCw0LLRgdGC0LLRg9C50YLQtSEiLCJtc2dfZ'.. 'GUiOiJHcsO8w58gR290dCJ9' it( "should work with the msg example", function() assert.is.same( msg64, basexx.to_url64( msgtxt ) ) assert.is.same( msgtxt, basexx.from_url64( msg64 ) ) end) it( "should allow to ignore characters in a url64 string", function() assert.is.same( "Man", basexx.from_url64( "TW-Fu\n", "-\n" ) ) end) end) basexx-0.3.0/test/z85_spec.lua000066400000000000000000000015001270324735500161320ustar00rootroot00000000000000basexx = require( "basexx" ) describe( "should handle ZeroMQ base85 strings", function() it( "should fulfill spec test case", function() -- http://rfc.zeromq.org/spec:32 local data = string.char( 0x86, 0x4f, 0xd2, 0x6f, 0xb5, 0x59, 0xf7, 0x5b ) local z85 = "HelloWorld" assert.is.same( z85, basexx.to_z85( data ) ) assert.is.same( data, basexx.from_z85( z85 ) ) end) it( "should encode a numeric string correctly", function() -- https://github.com/msealand/z85.node/blob/master/test/encode.test.js assert.is.same( "f!$Kw", basexx.to_z85( "1234" ) ) assert.is.same( "1234", basexx.from_z85( "f!$Kw" ) ) end) it( "should allow to ignore characters in a base85 string", function() assert.is.same( "1234", basexx.from_z85( "'f!$Kw'\n", "'\n" ) ) end) end)