PHPackages                             duzun/p2peg - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. duzun/p2peg

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

duzun/p2peg
===========

Peer to Peer Entropy Generator (or Random numbers generator with p2p seeding)

0.6.6(7y ago)31392MITPHPPHP &gt;=5.0.0CI failing

Since Jun 4Pushed 4y ago1 watchersCompare

[ Source](https://github.com/duzun/P2PEG)[ Packagist](https://packagist.org/packages/duzun/p2peg)[ Docs](https://github.com/duzun/P2PEG)[ RSS](/packages/duzun-p2peg/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (15)Used By (0)

Peer to Peer Entropy Generator
==============================

[](#peer-to-peer-entropy-generator)

or Random numbers generator with p2p seeding
--------------------------------------------

[](#or-random-numbers-generator-with-p2p-seeding)

@version 0.6.6 [![Build Status](https://camo.githubusercontent.com/9014cbe5acb8a34c91c5d57491c767b84781e5f5f03e5b70b30c2eafc062ced1/68747470733a2f2f7472617669732d63692e636f6d2f64757a756e2f50325045472e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/duzun/P2PEG)

[API Documentation](https://duzun.github.io/P2PEG/docs/)

About
-----

[](#about)

**Node**: There is a JavaScript version of this library under development [p2peg.js](https://github.com/duzun/p2peg.js).

This class uses a combination of sources of entropy to generate random data as unpredictable as possible. The key concept is sharing of random data between peers, where both peers benefit from the request.

Internally each peer generates random data using some system data, server performance/load, some [PRNGs](http://en.wikipedia.org/wiki/Pseudorandom_number_generator) (Pseudo Random Number Generators), timing and client supplied data. The collected data is always combined with the internal state data, which changes at each request, and digested by a [HMAC](https://en.wikipedia.org/wiki/Hash-based_message_authentication_code) with the peer's secret key.

Each client-peer adds to the entropy of the server-peer by suppling variable data with the request (in purpos or not) and by the fact of connecting to the server (the exact request time is also accounted), thus changing internal state of the `P2PEG`. The internal state is the sum of all collected entropy bits from system and from all client-peers that have ever requested current peer.

For client-peer there is no way to know about `P2PEG`'s internal state or about other client-peers, hence generated data is truly random and unpredictable.

If one peer doesn't trust the other peer to be "honest", it can contact multiple peers to gather the random bits and combine the result with it's own PRN and internal state.

On a web-server entropy can also be collected from common website clients.

Basic Usage
-----------

[](#basic-usage)

Include the class

```
require_once "/path/to/lib/P2PEG.php";
```

Get the singleton instance or just create a new instance of P2PEG

```
$P2PEG = P2PEG::instance();
```

Get some random binary string

```
$str = $P2PEG->str($length);
```

Now you can use `$str` as cryptographic salt, seed for PRNG, password generators or anything else that requires unpredictable hight entropy data.

Get some random integer numbers:

```
$int1 = $P2PEG->int();
$int2 = $P2PEG->int16();
$int3 = $P2PEG->int32();
$int4 = $P2PEG->rand($min, $max, $algo="int"); // see PHP's rand() in docs
```

Get some random text (base64 encoded)

```
$text = $P2PEG->text($length);
```

Get some random text (by a given alphabet)

```
$text = $P2PEG->alpha('a-z0-9AUOIE!', $length);
// or
$text = $P2PEG->text($length, 'a-z0-9AUOIE!');
```

Get some random string hex encoded

```
$hex = $P2PEG->hex($length);
```

Get a pseudo random 32bit integer - this method is faster then int32() for generating lots of numbers, but in turn it uses less entropy (see [RNG](http://en.wikipedia.org/wiki/Random_number_generation)).

```
$rand_int = $P2PEG->rand32($strict=true);
```

Get a pseudo random 64bit integer - this method is faster then int() for generating lots of numbers, but in turn it uses less entropy (see [xorshiftplus](http://vigna.di.unimi.it/ftp/papers/xorshiftplus.pdf) algorithm).

```
$rand_long = $P2PEG->rand64();
```

There are some more PRNGs, randomly seeded by P2PEG:

```
// x32
$P2PEG->xorShift32($strict=true);
$P2PEG->xorShift128($strict=true);
$P2PEG->xorwow($strict=true); // default in Nvidia's CUDA toolkit

// x64
$P2PEG->xorShift1024Star();
$P2PEG->xorShift128Plus();
$P2PEG->splitMix64();
```

Advanced Usage
--------------

[](#advanced-usage)

Before using the instance of `P2PEG` class, it is a good idea to set some properties:

Internal state file - optional. Tip: Keep it inaccessible to other users on system by `chmod 0600 p2peg.dat`

```
$P2PEG->state_file = "/path/to/data/p2peg.dat";
```

A secret key chosen at setup

```
$P2PEG->setSecret("some uniq secret that no one knows");
```

Seed the P2PEG with some bits of data or your choise

```
$P2PEG->seed("some (random) string");
```

Seed the PHP's RNG

```
mt_srand(crc32($P2PEG->seed()));
// or
mt_srand($P2PEG->int32());
```

Write to `/dev/random`

```
$P2PEG->seedRandomDev("some (optional) entropy");
```

Get a 56bit integer, if system is x64

```
$int64 = $P2PEG->int(7);
```

Display a random bitmap image

```
$P2PEG->servImg($width,$height,$method='rand32',$itemSize=0);
```

Take care of what `$method` you allow for `servImg()`, cause it could display some private data to client. The following methods are safe to display to client:

```
$allowMethods = array(
    // P2PEG entropy
    'int', 'int32', 'int16', 'str', 'seed', 'text', 'hex',
    // PRNGs seeded by P2PEG
    'rand32', 'xorShift32', 'xorShift128', 'xorwow',
    'rand64', 'xorShift128Plus', 'xorShift1024Star', 'splitMix64',
    // raw data
    'dynEntropy','clientEntropy','networkEntropy',
);
```

This method helps to visually inspect a random number generator (RNG). It is not enough to know how good the RNG is, but it can tell that the RNG is bad or something is wrong.

Examples:

-
-
-

Get some entropy from outside

```
$P2PEG->networkEntropy($autoseed=true);
```

On cron you could call

```
$P2PEG->expensiveEntropy($autoseed=true);
```

This method gathers some network entropy and server entropy and can be realy slow. This is why it is a good idea to call it in background. But at the same time, it is a good idea to call it from time to time, to get some more unpredictable, crtypto-safe entropy.

... more comming soon

Sample output
-------------

[](#sample-output)

[![Randomness Visualisation](https://camo.githubusercontent.com/c8ea29336952fc3a3d5b07fcdd26d696a55ae752d97b3c9fd33b7acfb2c28bf5/68747470733a2f2f64757a756e2e6d652f656e74726f70792f696d67)](https://camo.githubusercontent.com/c8ea29336952fc3a3d5b07fcdd26d696a55ae752d97b3c9fd33b7acfb2c28bf5/68747470733a2f2f64757a756e2e6d652f656e74726f70792f696d67)

Example of usage
----------------

[](#example-of-usage)

I've created a simple [password generator](https://duzun.me/pw) using P2PEG on both [client](https://github.com/duzun/p2peg.js) and [server](https://github.com/duzun/P2PEG). Each client is seeded by server, and server is seeded by each client, thus implementing P2PEG concept.

TODO
----

[](#todo)

1. To improve the entropy unpredictability, I intend to create system where multiple machines periodically exchange entropy. Each pear gets entropy and gives entropy at the same time with a simple GET request like this one:

    `curl "https://DUzun.Me/entropy/"`
2. Seed `/dev/random` and update entropy count, to improve system performance
3. Count the amount of entropy generated
4. Test the quality of entropy with [TestU01](http://simul.iro.umontreal.ca/testu01/tu01.html)
5. Create a JavaScript version (in development [here](https://github.com/duzun/p2peg.js))

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 78.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~85 days

Recently: every ~2 days

Total

14

Last Release

2895d ago

PHP version history (2 changes)0.3.1PHP &gt;=5.3.0

0.3.2PHP &gt;=5.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/5300c81d91f72d21119a70370ddf7810d64c38c81b677390eb2d63afe90e255d?d=identicon)[duzun](/maintainers/duzun)

---

Top Contributors

[![duzun](https://avatars.githubusercontent.com/u/321424?v=4)](https://github.com/duzun "duzun (93 commits)")[![duzunsys](https://avatars.githubusercontent.com/u/12141137?v=4)](https://github.com/duzunsys "duzunsys (26 commits)")

---

Tags

randomphpjavascriptcryptographyPRNGRNGseedentropyp2p

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/duzun-p2peg/health.svg)

```
[![Health](https://phpackages.com/badges/duzun-p2peg/health.svg)](https://phpackages.com/packages/duzun-p2peg)
```

###  Alternatives

[ircmaxell/random-lib

A Library For Generating Secure Random Numbers

84130.2M119](/packages/ircmaxell-random-lib)[savvot/random

Deterministic pseudo-random generators library with dozens of useful functions and several sources of randomness

21243.7k5](/packages/savvot-random)[hisune/echarts-php

A php wrapper for echarts javascript libraries

327201.9k5](/packages/hisune-echarts-php)[delight-im/random

The most convenient way to securely generate anything random in PHP

2345.4k](/packages/delight-im-random)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
