Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Nonce
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 create
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 verify
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Implement nonce helper methods.
4 *
5 * @link       https://automattic.com
6 * @since      1.0.0
7 * @package    automattic/jetpack-boost
8 */
9
10namespace Automattic\Jetpack_Boost\Lib;
11
12/**
13 * Class Nonce
14 */
15class Nonce {
16    /**
17     * This is a light clone of wp_create_nonce and wp_verify_nonce which skips the UID and cookie token parts,
18     * so it can be used in anonymous HTTP callbacks. It is therefore not as secure, so be careful.
19     *
20     * @param string $action The action.
21     */
22    public static function create( $action ) {
23        return substr( wp_hash( wp_nonce_tick() . '|' . $action, 'nonce' ), -12, 10 );
24    }
25
26    /**
27     * Verify the nonce.
28     *
29     * @param string $nonce  The nonce.
30     * @param string $action The action.
31     */
32    public static function verify( $nonce, $action ) {
33        $i = wp_nonce_tick();
34
35        // Current nonce.
36        $expected = substr( wp_hash( $i . '|' . $action, 'nonce' ), -12, 10 );
37        if ( hash_equals( $expected, $nonce ) ) {
38            return 1;
39        }
40
41        // Nonce generated 12-24 hours ago.
42        $expected = substr( wp_hash( ( $i - 1 ) . '|' . $action, 'nonce' ), -12, 10 );
43        if ( hash_equals( $expected, $nonce ) ) {
44            return 2;
45        }
46
47        return false;
48    }
49}