Skip to content

Instantly share code, notes, and snippets.

@roblogic
Last active May 28, 2020 03:23
Show Gist options
  • Select an option

  • Save roblogic/6e1cba0c979c4f08711cf3945df8472f to your computer and use it in GitHub Desktop.

Select an option

Save roblogic/6e1cba0c979c4f08711cf3945df8472f to your computer and use it in GitHub Desktop.
Z85 encoding implemented in Zsh.. Input: binary string (length 32). Output: Z85 ascii-encoded string (length 5). Spec: https://rfc.zeromq.org/spec:32/Z85/
#!/bin/zsh
# Version 0.00.01 - bare bones only
# Binary to Ascii conversion, using Z85 encoding algorithm
# Similar to Ascii85, few different characters, doesn't use ascii code table
# https://www.johndcook.com/blog/2019/03/05/base85-encoding/
# https://rfc.zeromq.org/spec:32/Z85/
# Example: (input must be 32 characters)
# input 01001101011000010110111000100000
# base85 24 73 80 78 61
# Z85 o&{[Z
#set -x
#Translation table
Z=({0..9} {a..z} {A..Z} \" . - : + = ^ ! / \* \? \& \< \> \( \) [ ] { \} @ % \$ \# )
#Conversion to Z85
#binary ==> radix 85 ==> Z85 ascii
X=$(for n (`bc<<<"obase=85;ibase=2;$1"`)printf %s $Z[$n+1])
#left pad with 0 if there are fewer than 5 digits
p=$[5-${#X}]
printf %s ${(l:$p::0:)}$X
0000001010101000000000000101000001010000000100100010001000100101100101010101010101010010010000000000000000000000000000000000000110000000000000000000110100000000000000000001101000000000000000000101010000000000000000001111100000000000000000000000000000000110000111000110000110001000000000000011001000011010001100011000011010111110111110111110111110000000000000000000000000010000000000000000010000000000000000000000000000100000000000000000111111000000000000011111000000000000000000000001100001100001110001100010000000100000000010000110100001100011100110101111101111101111101111100000000000000000000000000100000011000000000100000000000110000000000000001000001100000000001111110000011000000111110000000000110000000000000100000000100000000100000100000011000000010000000110000110000001000000000011000100001100000000000000011001100000000000001100010000110000000001100001100000010000000100000010000000010000010000000110000000010001000000001100000000100010000000001000000010000010000000100000001000000010000000000001100000000011000000001100000000010001110101100000000000100000001000000000000001000001111100000000000010000101110100101101100000010011100100111111101110000111000001101110000000001010000011101100100000010100000111111001000000101000001100000010000011011000000000000000000000000000000000001110000010000000000000011101010001010101010100111000000000101010100000000000000001010000000000000011111000000000000000011111111100000000000011100000001110000000001100000000000110000000110100000000010110000011001100000001100110000100010100000101000100001000100100010010001000000001000101000100000000000010000100001000000000000100000000010000000000000010010100000000000111100111110100111100000000000000000000
@roblogic
Copy link
Author

BUG: The double quote character (") is not valid Z85 and should not be in the translation table!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment