# Source of file Nonce.php

Size: 1,255 Bytes - Last Modified: 2016-08-28T19:38:31+00:00

## /Users/dshafik/src/akamai-open/edgegrid-auth-php/src/Authentication/Nonce.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
Covered by 4 test(s):

- Akamai\Open\EdgeGrid\Tests\Client\Authentication\NonceTest::testMakeNonce
- Akamai\Open\EdgeGrid\Tests\Client\Authentication\NonceTest::testMakeNonceRandomBytes
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultTimestamp
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultNonce

34 35 36
Covered by 4 test(s):

- Akamai\Open\EdgeGrid\Tests\Client\Authentication\NonceTest::testMakeNonce
- Akamai\Open\EdgeGrid\Tests\Client\Authentication\NonceTest::testMakeNonceRandomBytes
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultTimestamp
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultNonce

37
Covered by 4 test(s):

- Akamai\Open\EdgeGrid\Tests\Client\Authentication\NonceTest::testMakeNonce
- Akamai\Open\EdgeGrid\Tests\Client\Authentication\NonceTest::testMakeNonceRandomBytes
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultTimestamp
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultNonce

38 39
Covered by 4 test(s):

- Akamai\Open\EdgeGrid\Tests\Client\Authentication\NonceTest::testMakeNonce
- Akamai\Open\EdgeGrid\Tests\Client\Authentication\NonceTest::testMakeNonceRandomBytes
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultTimestamp
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultNonce

40 41 42 43 44 45 46 47 48 49
Covered by 3 test(s):

- Akamai\Open\EdgeGrid\Tests\Client\Authentication\NonceTest::testMakeNonce
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultTimestamp
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultNonce

50
Covered by 3 test(s):

- Akamai\Open\EdgeGrid\Tests\Client\Authentication\NonceTest::testMakeNonce
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultTimestamp
- Akamai\Open\EdgeGrid\Tests\Client\AuthenticationTest::testDefaultNonce

51 52 53

    <?php

    /**

     * Akamai {OPEN} EdgeGrid Auth for PHP

     *

     * @author Davey Shafik <dshafik@akamai.com>

     * @copyright Copyright 2015 Akamai Technologies, Inc. All rights reserved.

     * @license Apache 2.0

     * @link https://github.com/akamai-open/edgegrid-auth-php

     * @link https://developer.akamai.com

     * @link https://developer.akamai.com/introduction/Client_Auth.html

     */

    namespace Akamai\Open\EdgeGrid\Authentication;

    /**

     * Generates a random nonce for each request

     *

     * @package Akamai {OPEN} EdgeGrid Auth

     */

    class Nonce

    {

        /**

         * @var string The current random function to use

         */

        protected $function;

        /**

         * Constructor

         *

         * Determines if PHP 7's random_bytes() can be used

         */

        public function __construct()

        {

            $this->function = 'openssl_random_pseudo_bytes';

            // Use PHP 7's random_bytes()

            if (function_exists('random_bytes')) {

                $this->function = 'random_bytes';

            }

        }

        /**

         * Return the nonce when cast to string

         *

         * @return string Returns the nonce

         */

        public function __toString()

        {

            // because ($this->function)() won't work til PHP 7 :(

            $function = $this->function;

            return bin2hex($function(16));

        }

    }