代码之家  ›  专栏  ›  技术社区  ›  Malfist

如何创建和使用nonce

  •  60
  • Malfist  · 技术社区  · 14 年前

    我正在运行一个网站,并且有一个计分系统,可以根据你玩游戏的次数给你打分。

    它使用哈希来证明评分HTTP请求的完整性,这样用户就不能更改任何东西,但是我担心可能会发生,有人发现他们不需要更改它,他们只需要获得高分,并复制HTTP请求、头和所有内容。

    5 回复  |  直到 5 年前
        1
  •  58
  •   EasyCo    7 年前

    1. PHP Nonce Library
    2. OpenID Nonce Library

    WikiPedia page

    getNonce() {
        $id = Identify Request //(either by username, session, or something)
        $nonce = hash('sha512', makeRandomString());
        storeNonce($id, $nonce);
        return $nonce to client;
    }
    
    verifyNonce($data, $cnonce, $hash) {
        $id = Identify Request
        $nonce = getNonce($id);  // Fetch the nonce from the last request
        removeNonce($id, $nonce); //Remove the nonce from being used again!
        $testHash = hash('sha512',$nonce . $cnonce . $data);
        return $testHash == $hash;
    }
    

    sendData($data) {
        $nonce = getNonceFromServer();
        $cnonce = hash('sha512', makeRandomString());
        $hash = hash('sha512', $nonce . $cnonce . $data);
        $args = array('data' => $data, 'cnonce' => $cnonce, 'hash' => $hash);
        sendDataToClient($args);
    }
    

    makeRandomString hash('sha512', $nonce . $cnonce . $data);

    function makeRandomString($bits = 256) {
        $bytes = ceil($bits / 8);
        $return = '';
        for ($i = 0; $i < $bytes; $i++) {
            $return .= chr(mt_rand(0, 255));
        }
        return $return;
    }
    
        2
  •  19
  •   Scott Arciszewski    8 年前

    CAESAR

    1. 1
    2. NaCl

    $factory = new RandomLib\Factory;
    $generator = $factory->getMediumStrengthGenerator();
    $_SESSION['nonce'] [] = $generator->generate(32);
    

    $_SESSION['nonce'] []= random_bytes(32);
    

    $found = array_search($nonce, $_SESSION['nonces']);
    if (!$found) {
        throw new Exception("Nonce not found! Handle this or the app crashes");
    }
    // Yay, now delete it.
    unset($_SESSION['nonce'][$found]);
    

    array_search()

    // Generating a message bearing a nonce
    $nonce = random_bytes(32);
    $expires = new DateTime('now')
        ->add(new DateInterval('PT01H'));
    $message = json_encode([
        'nonce' => base64_encode($nonce),
        'expires' => $expires->format('Y-m-d\TH:i:s')
    ]);
    $publishThis = base64_encode(
        hash_hmac('sha256', $message, $authenticationKey, true) . $message
    );
    
    // Validating a message and retrieving the nonce
    $decoded = base64_decode($input);
    if ($decoded === false) {
        throw new Exception("Encoding error");
    }
    $mac = mb_substr($decoded, 0, 32, '8bit'); // stored
    $message = mb_substr($decoded, 32, null, '8bit');
    $calc = hash_hmac('sha256', $message, $authenticationKey, true); // calcuated
    if (!hash_equals($calc, $mac)) {
        throw new Exception("Invalid MAC");
    }
    $message = json_decode($message);
    $currTime = new DateTime('NOW');
    $expireTime = new DateTime($message->expires);
    if ($currTime > $expireTime) {
        throw new Exception("Expired token");
    }
    $nonce = $message->nonce; // Valid (for one hour)
    

    JSON Web Tokens

        3
  •  1
  •   Maurycy    14 年前

        4
  •  0
  •   oleviolin    5 年前

    function makeNonce($seed,$i=0){
        $timestamp = time();
        $q=-3; 
        //The epoch time stamp is truncated by $q chars, 
        //making the algorthim to change evry 1000 seconds
        //using q=-4; will give 10000 seconds= 2 hours 46 minutes usable time
    
        $TimeReduced=substr($timestamp,0,$q)-$i; 
    
        //the $seed is a constant string added to the string before hashing.    
        $string=$seed.$TimeReduced;
        $hash=hash('sha1', $string, false);
        return  $hash;
    }   
    

    if($nonce==$this->makeNonce($seed,0)||$nonce==$this->makeNonce($seed,1)){
         //handle data here
     } else {
         //reject nonce code   
     }