140 lines
4.7 KiB
PHP
140 lines
4.7 KiB
PHP
<?php
|
|
/** Salesforce Api Url */
|
|
define("IPER_SALESFORCE_API_URL","https://connectamerica--full.sandbox.my.salesforce-sites.com/RestServices/services/apexrest");
|
|
|
|
/** Salesforce Brand ID */
|
|
define("IPER_SALESFORCE_BRAND","MedicalAlert");
|
|
|
|
/** Encryption key */
|
|
define("IPER_ENCRYPTION_KEY","potEgc8+5f32y+jpXSz/NqFEPuVWoT95V7aYiyRNjpQ=");
|
|
|
|
/** Signature */
|
|
define("IPER_SIGNATURE_KEY","-----BEGIN PRIVATE KEY-----
|
|
MIICcgIBADANBgkqhkiG9w0BAQEFAASCAlwwggJYAgEAAoGAwCSPvGdoZsC1Q4btJETb9fnkM/ne
|
|
zBA4F4f0bX3JymVZ83H9F1CTykhQWjZ8WiAuPFGHNaUESGtfr0pWF113KrY5ei910WcvcBKd1w6w
|
|
JrpUdhWC5bAgoXfLoS0itbX7TvIKrvoXcHbtAPMEDyMNv/Dy/RstNTqUBzF2fLKTwTkCAwEAAQKB
|
|
gAZuct0554sLnPBOCOFePgVFaw0OVRXRiSnIeYxcry92Ja+ku3WsqHXB5pFOd5UbX1DSOHYO4vjQ
|
|
QfCqdVKXj1CGF+5snE8elE8uuN4Y7OgZ9PBdTJ/V2gevtkYQsjtjteZn3ay3Eic1ItVWSXL4NjZh
|
|
4eso3QN+yQsfUhFAOD59AkD1W58VDN8vHWymyJebIGPwKFf6bQpUCeEJVCR5gBvz0wGEQWaLoiyJ
|
|
HGSenfpDTo9eiKWvGOnlB11wLvvatqATAkDIeg5jQvj2M6aQRr1k+UETGtjz9BN5vwwpJt+qnVbR
|
|
hypoSsBtqBMxTSbWizNzMen1JaOw/Vck8Iei4FoatrsDAkAnSZp5hmweYTnKowgToOYfyHX99YPX
|
|
3RUZp02H3wmay0jM4qQG69rxwYgjFezC5ktyubK+DOE2+SzvD7boWKHdAkBup+B1LaxZyRyxGjrE
|
|
F0iyEOmbjieJ1cgSluByPjKDqMXhlxEr9c/SMLG1TlRxyyVGKSZ3NP765sEXSBq0EBSdAkCX8h79
|
|
z9mezZxyRcdod2Sk4t1hWUf0AnLhkzfAgdQoNwY692uBYXsyKXGufLNkb+RznASmn5Lr6NanIL4c
|
|
6S9P
|
|
-----END PRIVATE KEY-----");
|
|
|
|
|
|
|
|
if(!class_exists("EncryptedContent")){
|
|
class EncryptedContent{
|
|
public $IV;
|
|
public $Data;
|
|
}
|
|
}
|
|
|
|
if(!class_exists("MessageContentSigned")){
|
|
class MessageContentSigned{
|
|
public $Content;
|
|
public $Signature;
|
|
}
|
|
}
|
|
|
|
if (!class_exists("MessageContent")){
|
|
class MessageContent{
|
|
public $Body;
|
|
public $Timestamp;
|
|
public $Uid;
|
|
}
|
|
}
|
|
|
|
include_once ('message-content.php');
|
|
include_once ('message-content-signed.php');
|
|
include_once ('encrypted-content.php');
|
|
|
|
if(!class_exists("SecuredContent")){
|
|
date_default_timezone_set('UTC');
|
|
|
|
class SecuredContent {
|
|
protected $encrption_key;
|
|
protected $signature_key;
|
|
protected $cipher;
|
|
|
|
public function __construct() {
|
|
/*$this->encrption_key = get_option("encrption_key");
|
|
$this->signature_key = get_option("signature_key");*/
|
|
$this->encrption_key = IPER_ENCRYPTION_KEY;
|
|
$this->signature_key = IPER_SIGNATURE_KEY;
|
|
$this->cipher = 'AES-256-CBC';
|
|
|
|
}
|
|
|
|
public function encode_content($raw_content)
|
|
{
|
|
$message_content = new MessageContent();
|
|
$message_content->Body = $raw_content;
|
|
$message_content->Timestamp = date('Y-m-d H:i:s', time());
|
|
$message_content->Uid = self::guid();
|
|
|
|
// create signature & pack message
|
|
$signed_message = new MessageContentSigned();
|
|
$signed_message->Content = json_encode($message_content);
|
|
$signed_message->Signature = $this->generate_signature($signed_message->Content);
|
|
|
|
// create initialization vector & encode data
|
|
$iv_size = 16;
|
|
$iv = openssl_random_pseudo_bytes($iv_size);
|
|
$key = base64_decode($this->encrption_key);
|
|
$data = json_encode($signed_message);
|
|
$padding = 16 - (strlen($data) % 16);
|
|
$data .= str_repeat(chr($padding), $padding);
|
|
$cipher_text = openssl_encrypt($data, $this->cipher, $key, OPENSSL_RAW_DATA, $iv);
|
|
|
|
// store content inside an encrypted container
|
|
$encrypted_content = new EncryptedContent();
|
|
$encrypted_content->IV = base64_encode($iv);
|
|
$encrypted_content->Data = base64_encode($cipher_text);
|
|
|
|
return $encrypted_content;
|
|
}
|
|
|
|
public function decode_content($encrypted_string)
|
|
{
|
|
$encrypted_content = json_decode($encrypted_string);
|
|
|
|
// decode data
|
|
|
|
$key = base64_decode($this->encrption_key);
|
|
$iv = base64_decode($encrypted_content->IV);
|
|
$message = openssl_decrypt(base64_decode($encrypted_content->Data), $this->cipher, $key, OPENSSL_RAW_DATA, $iv);
|
|
$message = json_decode(substr($message, 0));
|
|
$signature = $this->generate_signature($message->Content);
|
|
|
|
return json_decode($message->Content)->Body;
|
|
}
|
|
|
|
private function generate_signature($message_content)
|
|
{
|
|
$private_key = openssl_get_privatekey($this->signature_key);
|
|
|
|
openssl_sign($message_content, $signature, $this->signature_key, 'SHA256');
|
|
|
|
openssl_free_key($private_key);
|
|
|
|
return base64_encode($signature);
|
|
}
|
|
|
|
public static function guid()
|
|
{
|
|
if (function_exists('com_create_guid') === true)
|
|
{
|
|
return trim(com_create_guid(), '{}');
|
|
}
|
|
|
|
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
|
|
}
|
|
}
|
|
}
|
|
|
|
|