Correct padding error

This commit is contained in:
David Ellefsen
2013-08-18 19:50:57 +02:00
parent 26c330e886
commit c5319e77a8
2 changed files with 30 additions and 9 deletions
+23 -2
View File
@@ -192,7 +192,11 @@ base58_to_integer([Char | Str]) ->
-spec base58_to_binary(base58()) -> binary().
base58_to_binary(Base58) ->
binary:encode_unsigned(base58_to_integer(Base58)).
Bin = binary:encode_unsigned(base58_to_integer(Base58)),
%The conversion between the binary and the integer strips any leading zero bytes that
% might have appeared in the binary - '0's' should be prepended to the binary stream for each
% 1 that appeared at the start of the base58 string.
zeroPad(Base58, Bin).
%% @doc Convert a binary into a Base58 encoded string. The resulting Base58
%% encoded string will be in a big-endian representation of the original binary.
@@ -203,5 +207,22 @@ base58_to_binary(Base58) ->
binary_to_base58(Binary) when is_binary(Binary) ->
case integer_to_base58(binary:decode_unsigned(Binary)) of
error -> error;
Base58 -> Base58
Base58 ->
% see above comment - just the reverse
binaryPad(binary_to_list(Binary), Base58)
end.
%% @doc Pad a "1" character to a Base58 stream to account for any stripped zeros
%%
%% @spec binaryPad(list(), base58())
binaryPad([0 | Rest], Bin) ->
binaryPad(Rest, "1" ++ Bin);
binaryPad(_, Bin) -> Bin.
%% @doc Pad a zero byte to a Base58 stream to account for any leading 1's
%%
%% @spec zeroPad(base58(), binary())
zeroPad("1" ++ Rest, Base58) ->
zeroPad(Rest, <<0, Base58/binary>>);
zeroPad(_, Base58) -> Base58.