Asked  8 Months ago    Answers:  5   Viewed   36 times

Is there a BigInteger class in PHP? If so, how do I access it or use it?



Hopefully helpfull links :


EDIT: Math_BigInteger

Example from :

Implements an arbitrary precision integer arithmetic library. Uses gmp or bcmath, if available, and an internal implementation, otherwise.


    $a = new Math_BigInteger(2);
    $b = new Math_BigInteger(3);

    $c = $a->add($b);

    echo $c->toString(); // outputs 5
Wednesday, March 31, 2021
answered 8 Months ago

In a word? No. There is no __equals magic method. There is a complete list of the magic methods in the manual.

You can do

$myObject1 == $myObject2

which will consider them equal if they have the same attributes and values, and are instances of the same class.

I have often wished for this type of method myself, but I think that a more useful one would be a __compare() method which would be called for any comparison operator <, >, ==, ===, etc it already exist for PHP's inbuilt classes as can be seen in the PHP internals wiki and there is an example of how it could be implemented in the PHPInternals book:-


int (*compare)(zval *object1, zval *object2 TSRMLS_DC)

Compares two objects. Used for the operators ==, !=, <, >, ⇐ and >=. The implementations should follow these rules – for any objects a, b and c that share the same compare handler:

One way I have used to achieve this is to implement a Comparable interface, something like:-

interface Comparable
     * @param Comparable $other
     * @return Int -1, 0 or 1 Depending on result of comparison
    public function compareTo(Comparable $other);

The details of object comparison, and everything else OOP related can be found here

This may be implemented in PHP 7.

There is now an implementation of this that you can install using composer.

Thursday, August 12, 2021
answered 3 Months ago

No, there is not a better solution. If you are working with values that cannot fit into a long or a double then you will need to use a reference type like BigInteger, and Java does not support operator overloading.

Technically, I suppose you could have some mapping between signed and unsigned values, but if your goal is clean and simple code then this is not at all the way to go.

Sunday, August 22, 2021
answered 2 Months ago

"I was wondering if there is an even better way to hash passwords in PHP 7+ then password_hash. Is password_hash good enough?"

Yes it is safe enough, and yes there is a better/safer way. As of PHP 7.2, Argon2 is part of a newly implemented (hashing) method that won the Password Hashing Competition which offers a more robust method, should you want to upgrade your version of PHP to 7.2.

The wiki on this states:

Argon2, the recommended password hashing algorithm by the Password Hashing Competition, is a modern algorithm for securely hashing passwords. Argon2 addresses several key downsides of existing algorithms in that it is designed for the highest memory filling rate, and effective use multiple computing units while still providing defense against tradeoff attacks. Unlike Bcrypt, which just takes a single cost factor, Argon2 is parameterized by three distinct factors:

  1. A memory cost that defines memory usage of the algorithm
  2. A time cost that defines the execution time of the algorithm and the number of iterations
  3. And a parallelism factor, which defines the number of parallel threads

You can also look into the following link which contains more information on Libsodium

The manual on also contains information on PASSWORD_ARGON2I.

The changelog states:

7.2.0 Support for Argon2 passwords using PASSWORD_ARGON2I was added.

If upgrading to PHP 7.2 is not an option, then you could increase the "cost".

Pulled from this answer and from the related post Generating Password Hash In PHP 5.5 And Setting Cost Option, and I quote:

Increasing the cost parameter by 1, doubles the needed time to calculate the hash value. The cost parameter is the logarithm (base-2) of the iteration count, that means:

$iterations = 2 ^ $cost;

You can also consult this other Q&A here on Stack Overflow:

  • How does password_hash really work?
Monday, August 23, 2021
answered 2 Months ago

Use .equals() as opposed to == when comparing objects. == will compare the objects' references, whereas .equals() will check to see if they have the same values. Since two objects will very rarely have the same reference you should never use == except for comparing primitive types (int, char, but String is not a primitive type!) where it doesn't matter.

So you want:

if (pkPublicExBI.equals(pkPublicExBIrecreated)) {

Instead of

if (pkPublicExBI == pkPublicExBIrecreated) {
Thursday, September 2, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :