Permalink
Browse files

added explicit copy() method to Crypt::GCrypt::MPI objects

git-svn-id: svn://localhost/Crypt-GCrypt/trunk@78 c2f821fb-fd85-dc11-8383-000bcdcb7a8f
  • Loading branch information...
1 parent 4b3342f commit 171727badb1b08e2cf4adf2c187d21c288a65ab1 dkg committed Jan 18, 2010
Showing with 33 additions and 1 deletion.
  1. +8 −0 GCrypt.xs
  2. +16 −0 lib/Crypt/GCrypt/MPI.pm
  3. +9 −1 t/20-mpi.t
View
@@ -946,6 +946,14 @@ cgm_cmp(gcma, gcmb)
OUTPUT:
RETVAL
+Crypt_GCrypt_MPI
+copy(gcm)
+ Crypt_GCrypt_MPI gcm;
+ CODE:
+ RETVAL=gcry_mpi_copy(gcm);
+ OUTPUT:
+ RETVAL
+
void
cgm_add(gcma, gcmb)
Crypt_GCrypt_MPI gcma;
@@ -98,6 +98,22 @@ Compares this object against another Crypt::GCrypt::MPI object,
returning 0 if the two values are equal, positive if this value is
greater, negative if $other is greater.
+=head2 copy()
+
+Returns a new Crypt::GCrypt::MPI object, with the contents identical
+to this one. This is distinct from using the assignment operator (=).
+For example:
+
+ $b = new Crypt::GCrypt::MPI(15);
+ $a = $b;
+ $b->add(1); # $a points to the same object, so both $a and $b contain 16.
+
+ $a = $b->copy(); # $a and $b are both 16, but different objects
+ $b->add(1); # $a == 16, $b == 17
+
+If $b is a Crypt::GCrypt::MPI object, then "$a = $b->copy();" is
+identical to "$a = Crypt::GCrypt::MPI->new($b);"
+
=head1 CALCULATIONS
=head2 add($other)
View
@@ -4,7 +4,7 @@
#########################
use Test;
-BEGIN { plan tests => 32 }; # <--- number of tests
+BEGIN { plan tests => 35 }; # <--- number of tests
use ExtUtils::testlib;
use Crypt::GCrypt::MPI;
@@ -99,3 +99,11 @@ ok(0 == $y->cmp($x));
$y->sub($thirtysix);
ok(0 == $y->cmp(Crypt::GCrypt::MPI->new(65501)));
ok(0 == $x->cmp(Crypt::GCrypt::MPI->new(65537)));
+
+# test copy method:
+$y = $x->copy();
+ok(0 == $y->cmp($x));
+$y->sub($thirtysix);
+ok(0 == $y->cmp(Crypt::GCrypt::MPI->new(65501)));
+ok(0 == $x->cmp(Crypt::GCrypt::MPI->new(65537)));
+

0 comments on commit 171727b

Please sign in to comment.