Reservoir

xubaiw/BaseN.lean

RFC 4648 compliant Base-N encoding in Lean 4

GitHub Link Documentation

Pick a version!

TODO: should show a time axes that allows the users to view, filter and click copy the lakefile config.

README

BaseN.lean

Encoding and decoding for base16, base32, base32hex, base64 and base64url, compliant to RFC 4648.

Usage

.map String.fromUTF8Unchecked -- some "Man" #eval encode64 "Ma".toUTF8 -- "TWE=" #eval decode64 "TWE=" |>.map String.fromUTF8Unchecked -- some "Ma"">
import BaseN
open BaseN

#eval encode64 "Man".toUTF8  -- "TWFu"
#eval decode64 "TWFu" |>.map String.fromUTF8Unchecked  -- some "Man"

#eval encode64 "Ma".toUTF8  -- "TWE="
#eval decode64 "TWE=" |>.map String.fromUTF8Unchecked  -- some "Ma"

To customize alphabet and other configutation:

.map String.fromUTF8Unchecked">
let myAlphabet := {
  list := "ABCDEFGHIJKLMNOPQRSTUV0123456789".data  -- length of `list` can be 16, 32 or 64
  padChar := '='  -- padChar can be `none` when list.length = 16
}  -- all character in `list` and `padChar` should be distinct to each other

def myEncode32 := encodeConfig {
  alphabet := myAlphabet
  usePaddding := false  -- do not pad `=` at the end
}

def myDecode32 := decodeConfig {
  alphabet := myAlphabet
  rejectOutside := false  -- ignore characters not in alphabet, otherwise fail the decoding
}

#eval myEncode32 "Ma".toUTF8
#eval myDecode32 "E70B" |>.map String.fromUTF8Unchecked