Encode-Base32-Crockford

 view release on metacpan or  search on metacpan

lib/Encode/Base32/Crockford.pm  view on Meta::CPAN

	# For each base32 digit B of position P counted (using zero-based counting)
	# from right in a number, its decimal value D is calculated with the
	# following expression:
	# 	D = B * 32^P.
	# As any number raised to the power of 0 is 1, we can define an "offset" value
	# of 1 for the first digit calculated and simply multiply the offset by 32
	# after deriving the value for each digit.

	foreach my $symbol (split(//, $string)) {
		$total = $total * 32 + $SYMBOLS{$symbol};
	}
	
	$total;
}

sub base32_decode_with_checksum {
	my ($string, $options) = @_;
	my $check_string = $string;

	my $checksum = substr($check_string, (length($check_string) - 1), 1, "");

	my $value = base32_decode($check_string, $options);
	my $checksum_value = base32_decode($checksum, { "is_checksum" => 1 });
	my $modulo = $value % 37;

	croak qq(Checksum symbol "$checksum" is not correct for value "$check_string".)
		if $checksum_value != $modulo;
	
	$value;
}

1;

__END__

=head1 NAME

Encode::Base32::Crockford - encode/decode numbers using Douglas Crockford's Base32 Encoding

=head1 VERSION

version 2.112991

=head1 DESCRIPTION

Douglas Crockford describes a I<Base32 Encoding> at 
L<http://www.crockford.com/wrmg/base32.html>. He says: "[Base32 Encoding is] a
32-symbol notation for expressing numbers in a form that can be conveniently and 
accurately transmitted between humans and computer systems."

This module provides methods to convert numbers to and from that notation.

=head1 SYNOPSIS

    use Encode::Base32::Crockford qw(:all); # import all methods

or

    use Encode::Base32::Crockford qw(base32_decode); # your choice of methods
    
    my $decoded = base32_decode_with_checksum("16JD");
    my $encoded = base32_encode_with_checksum(1234);

=head1 METHODS

=head2 base32_encode

    print base32_encode(1234); # prints "16J"

Encode a base 10 number. Will die on inappropriate input.

=head2 base32_encode_with_checksum

    print base32_encode_with_checksum(1234); # prints "16JD"

Encode a base 10 number with a checksum symbol appended. See the spec for a
description of the checksum mechanism. Wraps the previous method so will also
die on bad input.

=head2 base32_decode

    print base32_decode("16J"); # prints "1234"

    print base32_decode("IO", { mode => "warn"   }); # print "32" but warn
    print base32_decode("IO", { mode => "strict" }); # dies

Decode an encoded number into base 10. Will die on inappropriate input. The
function is case-insensitive, and will strip any hyphens in the input (see
C<normalize()>). A hashref of options may be passed, with the only valid option
being C<mode>. If set to "warn", normalization will produce a warning; if set
to "strict", requiring normalization will cause a fatal error.

=head2 base32_decode_with_checksum

    print base32_decode_with_checksum("16JD"); # prints "1234"

Decode an encoded number with a checksum into base 10. Will die if the checksum
isn't correct, and die on inappropriate input. Takes the same C<mode> option as
C<base32_decode>.

=head2 normalize

    my $string = "ix-Lb-Ko";
    my $clean = normalize($string);

    # $string is now '1X1BK0'.

The spec allows for certain symbols in encoded strings to be read as others, to
avoid problems with misread strings. This function will perform the following
conversions in the input string:

=over 4

=item * "i" or "I" to 1

=item * "l" or "L" to 1

=item * "o" or "O" to 0

=back



( run in 2.474 seconds using v1.01-cache-2.11-cpan-13bb782fe5a )