Defuse\Crypto\Crypto::legacyDecrypt PHP Метод

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

Decrypts a legacy ciphertext produced by version 1 of this library.
public static legacyDecrypt ( string $ciphertext, string $key ) : string
$ciphertext string
$key string
Результат string
    public static function legacyDecrypt($ciphertext, $key)
    {
        RuntimeTests::runtimeTest();
        // Extract the HMAC from the front of the ciphertext.
        if (Core::ourStrlen($ciphertext) <= Core::LEGACY_MAC_BYTE_SIZE) {
            throw new Ex\WrongKeyOrModifiedCiphertextException('Ciphertext is too short.');
        }
        $hmac = Core::ourSubstr($ciphertext, 0, Core::LEGACY_MAC_BYTE_SIZE);
        if ($hmac === false) {
            throw new Ex\EnvironmentIsBrokenException();
        }
        $ciphertext = Core::ourSubstr($ciphertext, Core::LEGACY_MAC_BYTE_SIZE);
        if ($ciphertext === false) {
            throw new Ex\EnvironmentIsBrokenException();
        }
        // Regenerate the same authentication sub-key.
        $akey = Core::HKDF(Core::LEGACY_HASH_FUNCTION_NAME, $key, Core::LEGACY_KEY_BYTE_SIZE, Core::LEGACY_AUTHENTICATION_INFO_STRING, null);
        if (self::verifyHMAC($hmac, $ciphertext, $akey)) {
            // Regenerate the same encryption sub-key.
            $ekey = Core::HKDF(Core::LEGACY_HASH_FUNCTION_NAME, $key, Core::LEGACY_KEY_BYTE_SIZE, Core::LEGACY_ENCRYPTION_INFO_STRING, null);
            // Extract the IV from the ciphertext.
            if (Core::ourStrlen($ciphertext) <= Core::LEGACY_BLOCK_BYTE_SIZE) {
                throw new Ex\WrongKeyOrModifiedCiphertextException('Ciphertext is too short.');
            }
            $iv = Core::ourSubstr($ciphertext, 0, Core::LEGACY_BLOCK_BYTE_SIZE);
            if ($iv === false) {
                throw new Ex\EnvironmentIsBrokenException();
            }
            $ciphertext = Core::ourSubstr($ciphertext, Core::LEGACY_BLOCK_BYTE_SIZE);
            if ($ciphertext === false) {
                throw new Ex\EnvironmentIsBrokenException();
            }
            // Do the decryption.
            $plaintext = self::plainDecrypt($ciphertext, $ekey, $iv, Core::LEGACY_CIPHER_METHOD);
            return $plaintext;
        } else {
            throw new Ex\WrongKeyOrModifiedCiphertextException('Integrity check failed.');
        }
    }

Usage Example

Пример #1
0
 /**
  * 1. VerifyHMAC-then-Decrypt the ciphertext to get the hash
  * 2. Verify that the password matches the hash
  *
  * @param string $password
  * @param string $ciphertext
  * @param string $aesKey - must be exactly 16 bytes
  * @return boolean
  */
 public static function decryptAndVerifyLegacy($password, $ciphertext, $aesKey)
 {
     if (self::safeStrlen($aesKey) !== 16) {
         throw new \Exception("Encryption keys must be 16 bytes long");
     }
     $hash = Crypto::legacyDecrypt($ciphertext, $aesKey);
     return \password_verify(\base64_encode(\hash('sha256', $password, true)), $hash);
 }
All Usage Examples Of Defuse\Crypto\Crypto::legacyDecrypt