Algorithm-IRCSRP2

 view release on metacpan or  search on metacpan

lib/Algorithm/IRCSRP2/Utils.pm  view on Meta::CPAN

    }
    die 'a <= candidate <= b' unless ($a <= $candidate && $candidate <= $b);

    return $candidate->bstr;
}

sub gen_a {
    my $n = Math::BigInt::->new(N());
    $n->bsub(1);
    return randint(2, $n);
}

sub int2bytes {
    my ($n) = @_;

    $n = $n->copy;

    if ($n == 0) { return 0x00 }

    my $x = '';

    while ($n) {
        $x = chr($n->copy->bmod(256)->bstr) . $x;
        $n->bdiv(256);
    }

    return $x;
}

sub bytes2int {
    my ($bytes) = @_;

    my @bs = split('', $bytes);

    my $n = Math::BigInt->new(0);

    foreach my $b (@bs) {
        $n->bmul(256);
        $n->badd(ord($b));
    }

    return $n;
}

sub xorstring {
    my ($a, $b, $blocksize) = @_;

    my $xored = '';

    my @as = split('', $a);
    my @bs = split('', $b);

    foreach my $i (@{[ 0 .. $blocksize - 1 ]}) {
        $xored .= chr(ord($as[$i]) ^ ord($bs[$i]));
    }

    return $xored;
}

sub padto {
    my ($msg, $length) = @_;

    my $L = length($msg);

    if ($L % $length) {
        $msg .= (chr(0) x ($length - $L % $length));
    }

    die('lenth($msg) % $length != 0') unless ((length($msg) % $length) == 0);

    return $msg;
}

sub hmac_sha256_128 {
    my ($key, $data) = @_;

    my $str = Digest::SHA::hmac_sha256($data, $key);
    $str = substr($str, 0, 16);

    return $str;
}

1;

__END__

=pod

=head1 NAME

Algorithm::IRCSRP2::Utils - Algorithm utility functions

=head1 VERSION

version 0.501

=head1 AUTHOR

Adam Flott <adam@npjh.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by Adam Flott.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut



( run in 0.707 second using v1.01-cache-2.11-cpan-140bd7fdf52 )