public function testSealFail() { $alice = KeyPair::generate(); $message = 'This is for your eyes only'; $sealed = Asymmetric::seal($message, $alice->getPublicKey(), true); // Let's flip one bit, randomly: $r = \Sodium\randombytes_uniform(\mb_strlen($sealed, '8bit')); $amt = 1 << \Sodium\randombytes_uniform(8); $sealed[$r] = \chr(\ord($sealed[$r]) ^ $amt); // This should throw an exception try { $opened = Asymmetric::unseal($sealed, $alice->getSecretKey(), true); $this->assertEquals($opened, $message); throw new Exception('ERROR: THIS SHOULD ALWAYS FAIL'); } catch (CryptoException\InvalidKey $e) { $this->assertTrue($e instanceof CryptoException\InvalidKey); } catch (CryptoException\InvalidMessage $e) { $this->assertTrue($e instanceof CryptoException\InvalidMessage); } }
/** * Save a key to a file * * @param Key|KeyPair $key * @param string $filename * @return bool */ public static function save($key, string $filename = '') : bool { if ($key instanceof KeyPair) { return self::saveKeyFile($filename, $key->getSecretKey()->getRawKeyMaterial()); } return self::saveKeyFile($filename, $key->getRawKeyMaterial()); }
/** * Save a key to a file * * @param Key|KeyPair $key * @param string $filename * @return int|boolean */ public static function save($key, $filename = '') { if ($key instanceof KeyPair) { return self::saveKeyFile($filename, $key->getSecretKey()->get()); } return self::saveKeyFile($filename, $key->get()); }