%% Copyright 2011 Steve Davis % % Licensed under the Apache License, Version 2.0 (the "License"); % you may not use this file except in compliance with the License. % You may obtain a copy of the License at % % http://www.apache.org/licenses/LICENSE-2.0 % % Unless required by applicable law or agreed to in writing, software % distributed under the License is distributed on an "AS IS" BASIS, % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % See the License for the specific language governing permissions and % limitations under the License. -module(base32). -export([encode/1, decode/1]). -define(BASE32_ALPHABET, { $A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L, $M, $N, $O, $P, $Q, $R, $S, $T, $U, $V, $W, $X, $Y, $Z, $2, $3, $4, $5, $6, $7 }). %% RFC 4648 %% encode(Bin) when is_binary(Bin) -> Split = 5 * (byte_size(Bin) div 5), <> = Bin, Main = << <<(b32e(C))>> || <> <= Main0 >>, encode0(Rest, Main). encode0(<<>>, Acc) -> Acc; encode0(<>, Acc) -> <>; encode0(<>, Acc) -> <>; encode0(<>, Acc) -> <>; encode0(<>, Acc) -> <>, Acc) -> Bits = decode0(X) bsr 2, <>; decode(<>, Acc) -> Bits = decode0(X) bsr 4, <>; decode(<>, Acc) -> Bits = decode0(X) bsr 1, <>; decode(<