Skip to content
This repository was archived by the owner on Jan 22, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions src/Bitpay/Crypto/OpenSSLExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static function hasSupport()
*/
public function getAlgos()
{
return openssl_get_cipher_methods();
}

/**
Expand Down Expand Up @@ -177,4 +178,85 @@ final public function saveCSRtoString($csr, $out, $notext = true)

return openssl_csr_export($csr, $out, $notext);
}
/**
*
* Encrypts $text based on your $key and $iv. The returned text is
* base-64 encoded to make it easier to work with in various scenarios.
* Default cipher is AES-256-CBC but you can substitute depending
* on your specific encryption needs.
*
* @param string $text
* @param string $key
* @param string $iv
* @param int $bit_check
* @param string $cipher_type
* @return string $text
* @throws Exception $e
*
*/
public function encrypt($text, $key = '', $iv = '', $bit_check = 8, $cipher_type = 'AES-256-CBC') {
try {
if (function_exists('openssl_pkey_new')) {
/* Ensure the key & IV is the same for both encrypt & decrypt. */
if (!empty($text) && is_string($text)) {
$text_num = str_split($text, $bit_check);
$text_num = $bit_check - strlen($text_num[count($text_num) - 1]);

for ($i = 0; $i < $text_num; $i++) {
$text = $text . chr($text_num);
}
$encrypted = openssl_encrypt($text, $cipher_type, $key, 0, $iv);
return base64_encode($encrypted);
} else {
return $text;
}
} else {
throw new \Exception('Error in OpenSSL encrypt: OpenSSL PHP extension missing. openssl_encrypt function not found.');

return false;
}
} catch (\Exception $e) {
throw $e;
}
}

/**
*
* Decrypts $text based on your $key and $iv. Make sure you use the same key
* and initialization vector that you used when encrypting the $text. Default
* cipher is AES-256-CBC but you can substitute depending on the cipher
* used for encrypting the text - very important.
*
* @param string $encrypted_text
* @param string $key
* @param string $iv
* @param int $bit_check
* @param string $cipher_type
* @return string $text
* @throws Exception $e
*
*/
public function decrypt($encrypted_text, $key = '', $iv = '', $bit_check = 8, $cipher_type = 'AES-256-CBC') {
try {
/* Ensure the key & IV is the same for both encrypt & decrypt. */
if (!empty($encrypted_text)) {

$decrypted = openssl_decrypt(base64_decode($encrypted_text), $cipher_type, $key, 0, $iv);
$last_char = substr($decrypted, -1);

for ($i = 0; $i < $bit_check; $i++) {
if (chr($i) == $last_char) {
$decrypted = substr($decrypted, 0, strlen($decrypted) - $i);
break;
}
}
return $decrypted;
} else {
return $encrypted_text;
}
} catch (\Exception $e) {
throw $e;
}
}

}
4 changes: 2 additions & 2 deletions src/Bitpay/PrivateKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public static function serializeSig($r, $s)
$byte = strrev($byte);

// msb check
if (Math::cmp('0x'.bin2hex($byte[0]), '0x80') >= 0) {
if (Math::cmp('0x'.bin2hex($byte[0]), '0'.'x80') >= 0) {
$byte = chr(0x00).$byte;
}

Expand All @@ -240,7 +240,7 @@ public static function serializeSig($r, $s)
$byte = strrev($byte);

// msb check
if (Math::cmp('0x'.bin2hex($byte[0]), '0x80') >= 0) {
if (Math::cmp('0x'.bin2hex($byte[0]), '0'.'x80') >= 0) {
$byte = chr(0x00).$byte;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Bitpay/PublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __toString()
return '';
}

if (Math::mod('0x'.$this->y, '0x02') == '1') {
if (Math::mod('0x'.$this->y, '0'.'x02') == '1') {
return sprintf('03%s', $this->x);
} else {
return sprintf('02%s', $this->x);
Expand Down