ParagonIE\Halite\Symmetric\Config::getConfig PHP Метод

getConfig() публичный статический Метод

Get the configuration
public static getConfig ( string $header, string $mode = 'encrypt' ) : self
$header string
$mode string
Результат self
    public static function getConfig(string $header, string $mode = 'encrypt') : self
    {
        if (Util::safeStrlen($header) < Halite::VERSION_TAG_LEN) {
            throw new CryptoException\InvalidMessage('Invalid version tag');
        }
        if (\ord($header[0]) !== 49 || \ord($header[1]) !== 66) {
            throw new CryptoException\InvalidMessage('Invalid version tag');
        }
        $major = \ord($header[2]);
        $minor = \ord($header[3]);
        if ($mode === 'encrypt') {
            return new Config(self::getConfigEncrypt($major, $minor));
        } elseif ($mode === 'auth') {
            return new Config(self::getConfigAuth($major, $minor));
        }
        throw new CryptoException\InvalidMessage('Invalid configuration mode: ' . $mode);
    }

Usage Example

Пример #1
0
 /**
  * Encrypt a message using the Halite encryption protocol
  * 
  * @param string $plaintext
  * @param EncryptionKey $secretKey
  * @param boolean $raw Don't hex encode the output?
  * @return string
  */
 public static function encrypt($plaintext, Contract\KeyInterface $secretKey, $raw = false)
 {
     if (!$secretKey instanceof EncryptionKey) {
         throw new CryptoException\InvalidKey('Expected an instance of EncryptionKey');
     }
     $config = SymmetricConfig::getConfig(Halite::HALITE_VERSION, 'encrypt');
     $nonce = \Sodium\randombytes_buf(\Sodium\CRYPTO_SECRETBOX_NONCEBYTES);
     $salt = \Sodium\randombytes_buf($config->HKDF_SALT_LEN);
     list($eKey, $aKey) = self::splitKeys($secretKey, $salt, $config);
     $xored = \Sodium\crypto_stream_xor($plaintext, $nonce, $eKey);
     $auth = self::calculateMAC(Halite::HALITE_VERSION . $salt . $nonce . $xored, $aKey);
     \Sodium\memzero($eKey);
     \Sodium\memzero($aKey);
     if (!$raw) {
         return \Sodium\bin2hex(Halite::HALITE_VERSION . $salt . $nonce . $xored . $auth);
     }
     return Halite::HALITE_VERSION . $salt . $nonce . $xored . $auth;
 }
All Usage Examples Of ParagonIE\Halite\Symmetric\Config::getConfig