Example #1
0
 /**
  * @expectedException \Defuse\Crypto\Exception\BadFormatException
  * @excpectedExceptionMessage key version header
  */
 public function testIncorrectHeader()
 {
     $key = Key::createNewRandomKey();
     $str = $key->saveToAsciiSafeString();
     $str[0] = 'f';
     Key::loadFromAsciiSafeString($str);
 }
Example #2
0
 /**
  * Creates instance
  *
  * @param string $key The encryption key
  *
  * @throws \CodeCollab\Encryption\CryptoException When the key is not in a valid format
  */
 public function __construct(string $key)
 {
     try {
         $this->key = DefuseKey::loadFromAsciiSafeString($key);
     } catch (BadFormatException $e) {
         throw new CryptoException($e->getMessage(), $e->getCode(), $e);
     }
 }
 /**
  * @param $key
  * @param array $options
  */
 public function __construct($key, array $options = [])
 {
     if (is_string($key)) {
         $key = Key::loadFromAsciiSafeString($key);
     }
     $this->key = $key;
     $this->setOptions($options);
 }
Example #4
0
 /**
  * Decrypt with AES256.
  * This method relies on the settings in application config `AES_KEY256` which is generated at install to 
  * a unique key for your application. You can use a different key by passing it in at `$key256`.
  *
  *
  * @param  string $crypt Value to decrypt using AES256.
  * @param null|string $key256 An options key used to perform the decryption with instead of the `AES_KEY256`.
  *
  * @return string The decrypted value of $crypt.
  */
 public function decrypt($crypt, $key256 = null)
 {
     if ($key256 === null) {
         $key256 = \App::config('AES_KEY256');
     }
     //if
     $key256 = \Defuse\Crypto\Key::loadFromAsciiSafeString($key256);
     return \Defuse\Crypto\Crypto::decrypt($crypt, $key256);
 }
Example #5
0
 /**
  * Method to encrypt a data string.
  *
  * @param   string  $data  The data string to encrypt.
  * @param   Key     $key   The key object to use for encryption.
  *
  * @return  string  The encrypted data string.
  *
  * @since   1.3.0
  * @throws  \InvalidArgumentException
  * @throws  \RuntimeException
  */
 public function encrypt($data, Key $key)
 {
     // Validate key.
     if ($key->getType() != 'crypto') {
         throw new \InvalidArgumentException('Invalid key of type: ' . $key->getType() . '.  Expected crypto.');
     }
     // Encrypt the data.
     try {
         return DefuseCrypto::encrypt($data, DefuseKey::loadFromAsciiSafeString($key->getPrivate()));
     } catch (EnvironmentIsBrokenException $ex) {
         throw new \RuntimeException('Cannot safely perform encryption', $ex->getCode(), $ex);
     }
 }
Example #6
0
 /**
  * 
  * Encrypt a message with defuse/php-encryption, using an ephemeral key, 
  * then encrypt the key with RSA.
  * 
  * @param string $ciphertext
  * @param PrivateKey $rsaPrivateKey
  * 
  * @return string
  * @throws InvalidCiphertextException
  * @throws InvalidChecksumException
  */
 public static function decrypt($ciphertext, PrivateKey $rsaPrivateKey)
 {
     $split = explode(self::SEPARATOR, $ciphertext);
     if (\count($split) !== 4) {
         throw new InvalidCiphertextException('Invalid ciphertext message');
     }
     if (!\hash_equals($split[0], self::VERSION_TAG)) {
         throw new InvalidCiphertextException('Invalid version tag');
     }
     $checksum = \substr(\hash('sha256', implode('$', array_slice($split, 0, 3))), 0, 16);
     if (!\hash_equals($split[3], $checksum)) {
         throw new InvalidChecksumException('Invalid checksum');
     }
     $key = Key::loadFromAsciiSafeString(self::rsaDecrypt(Base64::decode($split[1]), $rsaPrivateKey));
     return Crypto::Decrypt(Base64::decode($split[2]), $key, true);
 }
Example #7
0
 public static function generateCookieToken($login, $password)
 {
     $persistence = 0;
     $config = self::loadConfig();
     // Add a cookie for session persistance, if enabled
     if (isset($config['persistant_enable']) && $config['persistant_enable']) {
         if (trim($config['persistant_encryption_key']) == '' || trim($config['persistant_cookie_name']) == '') {
             jLog::log(jLocale::get('jelix~auth.error.persistant.incorrectconfig', 'persistant_cookie_name, persistant_encryption_key'), 'error');
             return 0;
         }
         if (isset($config['persistant_duration'])) {
             $persistence = intval($config['persistant_duration']) * 86400;
         } else {
             $persistence = 86400;
             // 24h
         }
         $persistence += time();
         $cryptokey = \Defuse\Crypto\Key::loadFromAsciiSafeString($config['persistant_encryption_key']);
         $encrypted = \Defuse\Crypto\Crypto::encrypt(json_encode(array($login, $password)), $cryptokey);
         setcookie($config['persistant_cookie_name'], $encrypted, $persistence, $config['persistant_cookie_path'], "", false, true);
     }
     return $persistence;
 }
Example #8
0
 public function createCachePool()
 {
     return new EncryptedCachePool(new ArrayCachePool(null, $this->cacheArray), Key::loadFromAsciiSafeString('def000007c57b06c65b0df4bcac939924e42605d8d76e1462b619318bf94107c28db30c5394b4242db5e45563e1226cffcdff8123fa214ea1fcc4aa10b0ddb1b4a587b7e'));
 }
 public static function createEncrypter(ContainerInterface $container) : Encrypter
 {
     $encrypt = new Encrypter(Key::loadFromAsciiSafeString(self::getConfig($container, 'key', '')));
     return $encrypt;
 }