Skip to content

Commit d9442fd

Browse files
committed
Added encryption func + test
1 parent b64f9cf commit d9442fd

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

src/Utils.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static function serialize($obj)
6666

6767
public static function decrypt($cipherText, $cipherKey)
6868
{
69-
$key = mb_convert_encoding(substr($cipherKey, 0, 32), "utf-8");
69+
$key = mb_convert_encoding(substr($cipherKey, 0, AES_KEY_SIZE), "utf-8");
7070
$contents = hex2bin($cipherText);
7171

7272
$iv = substr($contents, 0, BLOCK_SIZE);
@@ -79,4 +79,16 @@ public static function decrypt($cipherText, $cipherKey)
7979
return "";
8080
}
8181
}
82+
83+
public static function encrypt($plainText, $cipherKey) {
84+
$iv = openssl_random_pseudo_bytes(BLOCK_SIZE);
85+
86+
if ($encrypted = openssl_encrypt($plainText, ALGORITHM, $cipherKey, true, $iv)) {
87+
return bin2hex($iv) . bin2hex($encrypted);
88+
} else {
89+
Logger::debug("Decrypt error", openssl_error_string());
90+
return "";
91+
}
92+
93+
}
8294
}

tests/SecureNativeTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,21 @@ public function testBasicVerify()
5757
$this->assertEquals($response, new VerifyResult());
5858
}
5959

60-
public function testDecription() {
60+
public function testDecryption()
61+
{
6162
$cookie = "821cb59a6647f1edf597956243e564b00c120f8ac1674a153fbd707da0707fb236ea040d1665f3d294aa1943afbae1b26b2b795a127f883ec221c10c881a147bb8acb7e760cd6f04edc21c396ee1f6c9627d9bf1315c484a970ce8930c2ed1011af7e8569325c7edcdf70396f1abca8486eabec24567bf215d2e60382c40e5c42af075379dacdf959cb3fef74f9c9d15";
6263
$apikey = "6EA4915349C0AAC6F6572DA4F6B00C42DAD33E75";
6364
$d = Utils::decrypt($cookie, $apikey);
6465
$e = "{\"cid\":\"198a41ff-a10f-4cda-a2f3-a9ca80c0703b\",\"fp\":\"6d8cabd95987f8318b1fe01593d5c2a5.24700f9f1986800ab4fcc880530dd0ed\"}";
6566
$this->assertEquals($e, $d);
6667
}
68+
69+
public function testEncrypt()
70+
{
71+
$api_key = '6EA4915349C0AAC6F6572DA4F6B00C42DAD33E75';
72+
$data = "{\"cid\":\"198a41ff-a10f-4cda-a2f3-a9ca80c0703b\",\"fp\":\"6d8cabd95987f8318b1fe01593d5c2a5.24700f9f1986800ab4fcc880530dd0ed\"}";
73+
$enc = Utils::encrypt($data, $api_key);
74+
$dec = Utils::decrypt($enc, $api_key);
75+
$this->assertEquals($data, $dec);
76+
}
6777
}

0 commit comments

Comments
 (0)