This commit is contained in:
parent
0fd45ae1e7
commit
2d49209142
4 changed files with 16 additions and 8 deletions
16
src/b32.ml
16
src/b32.ml
|
|
@ -1,4 +1,3 @@
|
|||
(* TODO test *)
|
||||
(* Crockford's variant of Base32
|
||||
http://www.crockford.com/wrmg/base32.html
|
||||
except that:
|
||||
|
|
@ -6,11 +5,18 @@
|
|||
- '-' is not allowed
|
||||
- checksum is not allowed *)
|
||||
|
||||
(* 'I' 'L' 'O' 'U' excluded *)
|
||||
(* 'I' 'L' 'O' 'U' excluded
|
||||
no '=' padding in encoded string *)
|
||||
type t = string
|
||||
|
||||
let alphabet = Base32.make_alphabet "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
|
||||
let encode s = Base32.encode_string ~alphabet s
|
||||
|
||||
let encode s =
|
||||
let s = Base32.encode_string ~alphabet s in
|
||||
(* remove '=' padding *)
|
||||
match String.index_opt s '=' with
|
||||
| None -> s
|
||||
| Some i -> String.sub s 0 i
|
||||
|
||||
let decode s =
|
||||
let s =
|
||||
|
|
@ -23,6 +29,10 @@ let decode s =
|
|||
| c -> c)
|
||||
s
|
||||
in
|
||||
(* restore padding for base32 lib *)
|
||||
let n = 8 - (String.length s mod 8) in
|
||||
let pad = String.make n '=' in
|
||||
let s = s ^ pad in
|
||||
match Base32.decode ~alphabet ~off:0 ~len:(String.length s) s with
|
||||
| Error (`Msg e) -> Error e
|
||||
| Ok v -> Ok v
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue