Skip to content

Commit

Permalink
Merge pull request #79 from paragonie/v18-polyfill-fast
Browse files Browse the repository at this point in the history
Add ParagonIE_Sodium_Compat::polyfill_is_fast()
  • Loading branch information
paragonie-scott authored Nov 29, 2018
2 parents c499029 + f762a27 commit 50281bb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ There are three ways to make it fast:
without harming the security of your cryptography keys. If your processor *isn't* safe, then decide whether you
want speed or security because you can't have both.

### How can I tell if sodium_compat will be slow, at runtime?

Since version 1.8, you can use the `polyfill_is_fast()` static method to
determine if sodium_compat will be slow at runtime.

```php
<?php
if (ParagonIE_Sodium_Compat::polyfill_is_fast()) {
// Use libsodium now
$process->execute();
} else {
// Defer to a cron job or other sort of asynchronous process
$process->enqueue();
}
```

### Help, my PHP only has 32-Bit Integers! It's super slow!

Some features of sodium_compat are ***incredibly slow* with PHP 5 on Windows**
Expand Down
16 changes: 16 additions & 0 deletions src/Compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -2639,6 +2639,22 @@ public static function memzero(&$var)
);
}

/**
* Will sodium_compat run fast on the current hardware and PHP configuration?
*
* @return bool
*/
public static function polyfill_is_fast()
{
if (extension_loaded('sodium')) {
return true;
}
if (extension_loaded('libsodium')) {
return true;
}
return PHP_INT_SIZE === 8;
}

/**
* Generate a string of bytes from the kernel's CSPRNG.
* Proudly uses /dev/urandom (if getrandom(2) is not available).
Expand Down

0 comments on commit 50281bb

Please sign in to comment.