1--TEST-- 2Crypto\Cipher::setTagLength in GCM mode basic usage. 3--SKIPIF-- 4<?php 5if (!Crypto\Cipher::hasMode(Crypto\Cipher::MODE_GCM)) 6 die("Skip: GCM mode not defined (update OpenSSL version)"); 7?> 8--FILE-- 9<?php 10$key = str_repeat('x', 32); 11$iv = str_repeat('i', 16); 12 13$data = str_repeat('a', 16); 14 15$cipher = new Crypto\Cipher('aes-256-gcm'); 16try { 17 $cipher->setTagLength(1); 18} 19catch (Crypto\CipherException $e) { 20 if ($e->getCode() == Crypto\CipherException::TAG_LENGTH_LOW) { 21 echo "LOW\n"; 22 } 23} 24 25$cipher = new Crypto\Cipher('aes-256-gcm'); 26try { 27 $cipher->setTagLength(100); 28} 29catch (Crypto\CipherException $e) { 30 if ($e->getCode() == Crypto\CipherException::TAG_LENGTH_HIGH) { 31 echo "HIGH\n"; 32 } 33} 34 35$cipher = new Crypto\Cipher('aes-256-gcm'); 36$cipher->decryptInit($key, $iv); 37try { 38 $cipher->setTagLength(12); 39} 40catch (Crypto\CipherException $e) { 41 if ($e->getCode() == Crypto\CipherException::TAG_LENGTH_SETTER_FORBIDDEN) { 42 echo "FLOW\n"; 43 } 44} 45 46$cipher = new Crypto\Cipher('aes-256-gcm'); 47$cipher->setTagLength(12); 48echo bin2hex($cipher->encrypt($data, $key, $iv)) . "\n"; 49echo bin2hex($cipher->getTag()) . "\n"; 50 51 52?> 53--EXPECT-- 54LOW 55HIGH 56FLOW 57622070d3bea6f720943d1198a7e6afa5 58ed39e13f9a9fdf19036ad2f1 59