Exemplo n.º 1
0
 /**
  * Generate a new keypair
  * 
  * @param int $type Key flags
  * @param &string $secret_key - Reference to optional variable to store secret key in
  * @return KeyPair
  * @throws CryptoException\InvalidKey
  */
 public static function generate($type = Key::CRYPTO_SIGN, &$secret_key = null)
 {
     if (Key::doesNotHaveFlag($type, Key::ASYMMETRIC)) {
         throw new CryptoException\InvalidKey('An asymmetric key type must be passed to KeyPair::generate()');
     }
     if (Key::hasFlag($type, Key::SIGNATURE)) {
         $key = SignatureSecretKey::generate(Key::CRYPTO_SIGN, $secret_key);
         $keypair = new SignatureKeyPair(...$key);
         return $keypair;
     }
     throw new CryptoException\InvalidKey('You must specify encryption or authentication flags.');
 }
Exemplo n.º 2
0
 /**
  * Generate a new keypair
  * 
  * @param int $type Key flags
  * @param &string $secret_key - Reference to optional variable to store secret key in
  * @return KeyPair
  * @throws CryptoException\InvalidKey
  */
 public static function generate($type = Key::CRYPTO_BOX, &$secret_key = null)
 {
     if (Key::doesNotHaveFlag($type, Key::ASYMMETRIC)) {
         throw new CryptoException\InvalidKey('An asymmetric key type must be passed to KeyPair::generate()');
     }
     if (Key::hasFlag($type, Key::ENCRYPTION)) {
         $key = EncryptionSecretKey::generate(Key::CRYPTO_BOX, $secret_key);
         $keypair = new EncryptionKeyPair(...$key);
         return $keypair;
     }
     throw new CryptoException\InvalidKey('Only encryption keys can be generated.');
 }