public static function verifyAuth($auth) { $array = explode(".", $auth); if (count($array) != 3) { return false; } $token = $array[0]; $time = $array[1]; $sign = $array[2]; $c = new \sodium\crypto(); $mysec = $c->keypair(); $mysec->load("d9e51b64202a4e5d45ae44aad312b2c800771d09f8335b8da664c9d8cc724345", "858f393c6446da67e5c3913ec66a8de3c9293f76c0d63d432e6852102eb9418d", true); $nonce = new \sodium\nonce(); $client_public = new \sodium\public_key(); $client_public->load("65a248a7e527d576d44b918cb3ae02303c9a206bfc2ec56cc135bb9e659e757c", true); $sign = $c->box_open(hex2bin($sign), $nonce->set_nonce(hex2bin('565870a7000bd8466f83d97a04333245000067dd443bbb4b'), false), $client_public, $mysec); $signArray = explode(":", $sign); if (count($signArray) != 2) { return false; } if ($token != $signArray[0]) { return false; } if ($time != $signArray[1]) { return false; } return $token; }
try { $c = new \sodium\crypto(); // Create a secret key $alice_secret = $c->keypair(); // Create public key $alice_public = new \sodium\public_key(); // Load binary key from alice_secret (pbin), false: expect binary, not key in hex $alice_public->load($alice_secret->pbin, false); // Alice's friend Bob $bob_secret = $c->keypair(); // Create public key from bob_secret (pbin) $bob_public = new \sodium\public_key(); $bob_public->load($bob_secret->pbin, false); // Alice's message to Bob $message = "Now Jesus did many other signs in the presence of the disciples,"; $message .= "which are not written in this book; but these are written so that"; $message .= "you may believe that Jesus is the Christ, the Son of God, and that"; $message .= "by believing you may have life in his name. (ESV, John 20:30:31)"; // Create a nonce $nonce = new \sodium\nonce(); // Every call to $nonce->next() generates a new nonce! Important for crypto_box // Use Bob's public key to send to Bob $encrypted_text = $c->box($message, $nonce->next(), $bob_public, $alice_secret); // Bob receives the $encrypted_text and 24 bytes nonce->nbin from Alice via the network $nonce_from_alice = $nonce->nbin; $bob_nonce = new \sodium\nonce(); $message_decrypted = $c->box_open($encrypted_text, $bob_nonce->set_nonce($nonce_from_alice, true), $alice_public, $bob_secret); echo "Messages encrypted/decrypted\n"; } catch (\sodium\crypto_exception $e) { syslog(LOG_ERR, sprintf("Error: %s:%s : (%s) %s\n%s\n", $e->getFile(), $e->getLine(), $e->getCode(), $e->getMessage(), $e->getTraceAsString())); }