Skip to content

Instantly share code, notes, and snippets.

@shkumagai
Created May 9, 2012 02:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shkumagai/2641298 to your computer and use it in GitHub Desktop.
Save shkumagai/2641298 to your computer and use it in GitHub Desktop.
How to use add_bits() function in Digest::SHA
use strict;
use warnings;
use Digest::SHA;
# Using sha256 algorism
our $ctx = Digest::SHA->new(256);
sub bit_to_digest {
my ($bits) = @_;
printf "Origin: %s\n", $bits;
printf "Digest: %s\n", $ctx->add_bits($bits)->hexdigest;
printf "Digest: %s\n", $ctx->add_bits(pack('B*', $bits), length($bits))->hexdigest;
}
sub main {
bit_to_digest($ARGV[0]);
}
# Usage:
# $ perl digest_sample.pl 111100001010
#
main() if (__FILE__ eq $0);
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment