@ -25,7 +25,6 @@
namespace OCA\Encryption;
namespace OCA\Encryption;
//require_once '../3rdparty/Crypt_Blowfish/Blowfish.php';
require_once realpath(dirname(__FILE__) . '/../3rdparty/Crypt_Blowfish/Blowfish.php');
require_once realpath(dirname(__FILE__) . '/../3rdparty/Crypt_Blowfish/Blowfish.php');
/**
/**
@ -86,7 +85,7 @@ class Crypt {
* blocks with encryption alone, hence padding is added to achieve the
* blocks with encryption alone, hence padding is added to achieve the
* required length.
* required length.
*/
*/
public static function addPadding($data) {
private static function addPadding($data) {
$padded = $data . 'xx';
$padded = $data . 'xx';
@ -99,7 +98,7 @@ class Crypt {
* @param string $padded padded data to remove padding from
* @param string $padded padded data to remove padding from
* @return string unpadded data on success, false on error
* @return string unpadded data on success, false on error
*/
*/
public static function removePadding($padded) {
private static function removePadding($padded) {
if (substr($padded, -2) === 'xx') {
if (substr($padded, -2) === 'xx') {
@ -207,7 +206,7 @@ class Crypt {
* @param string $passphrase
* @param string $passphrase
* @return string encrypted file content
* @return string encrypted file content
*/
*/
public static function encrypt($plainContent, $iv, $passphrase = '') {
private static function encrypt($plainContent, $iv, $passphrase = '') {
if ($encryptedContent = openssl_encrypt($plainContent, 'AES-128-CFB', $passphrase, false, $iv)) {
if ($encryptedContent = openssl_encrypt($plainContent, 'AES-128-CFB', $passphrase, false, $iv)) {
return $encryptedContent;
return $encryptedContent;
@ -228,7 +227,7 @@ class Crypt {
* @throws \Exception
* @throws \Exception
* @return string decrypted file content
* @return string decrypted file content
*/
*/
public static function decrypt($encryptedContent, $iv, $passphrase) {
private static function decrypt($encryptedContent, $iv, $passphrase) {
if ($plainContent = openssl_decrypt($encryptedContent, 'AES-128-CFB', $passphrase, false, $iv)) {
if ($plainContent = openssl_decrypt($encryptedContent, 'AES-128-CFB', $passphrase, false, $iv)) {
@ -248,7 +247,7 @@ class Crypt {
* @param string $iv IV to be concatenated
* @param string $iv IV to be concatenated
* @returns string concatenated content
* @returns string concatenated content
*/
*/
public static function concatIv($content, $iv) {
private static function concatIv($content, $iv) {
$combined = $content . '00iv00' . $iv;
$combined = $content . '00iv00' . $iv;
@ -261,7 +260,7 @@ class Crypt {
* @param string $catFile concatenated data to be split
* @param string $catFile concatenated data to be split
* @returns array keys: encrypted, iv
* @returns array keys: encrypted, iv
*/
*/
public static function splitIv($catFile) {
private static function splitIv($catFile) {
// Fetch encryption metadata from end of file
// Fetch encryption metadata from end of file
$meta = substr($catFile, -22);
$meta = substr($catFile, -22);
@ -378,34 +377,6 @@ class Crypt {
}
}
/**
* @brief Creates symmetric keyfile content using a generated key
* @param string $plainContent content to be encrypted
* @returns array keys: key, encrypted
* @note symmetricDecryptFileContent() can be used to decrypt files created using this method
*
* This function decrypts a file
*/
public static function symmetricEncryptFileContentKeyfile($plainContent) {
$key = self::generateKey();
if ($encryptedContent = self::symmetricEncryptFileContent($plainContent, $key)) {
return array(
'key' => $key,
'encrypted' => $encryptedContent
);
} else {
return false;
}
}
/**
/**
* @brief Create asymmetrically encrypted keyfile content using a generated key
* @brief Create asymmetrically encrypted keyfile content using a generated key
* @param string $plainContent content to be encrypted
* @param string $plainContent content to be encrypted
@ -488,43 +459,11 @@ class Crypt {
}
}
/**
* @brief Asymetrically encrypt a string using a public key
* @param $plainContent
* @param $publicKey
* @return string encrypted file
*/
public static function keyEncrypt($plainContent, $publicKey) {
openssl_public_encrypt($plainContent, $encryptedContent, $publicKey);
return $encryptedContent;
}
/**
* @brief Asymetrically decrypt a file using a private key
* @param $encryptedContent
* @param $privatekey
* @return string decrypted file
*/
public static function keyDecrypt($encryptedContent, $privatekey) {
$result = @openssl_private_decrypt($encryptedContent, $plainContent, $privatekey);
if ($result) {
return $plainContent;
}
return $result;
}
/**
/**
* @brief Generates a pseudo random initialisation vector
* @brief Generates a pseudo random initialisation vector
* @return String $iv generated IV
* @return String $iv generated IV
*/
*/
public static function generateIv() {
private static function generateIv() {
if ($random = openssl_random_pseudo_bytes(12, $strong)) {
if ($random = openssl_random_pseudo_bytes(12, $strong)) {
@ -550,7 +489,7 @@ class Crypt {
}
}
/**
/**
* @brief Generate a pseudo random 1024kb ASCII key
* @brief Generate a pseudo random 1024kb ASCII key, used as file key
* @returns $key Generated key
* @returns $key Generated key
*/
*/
public static function generateKey() {
public static function generateKey() {
@ -576,13 +515,13 @@ class Crypt {
}
}
/**
/**
* @brief Get the blowfish encryption hande ler for a key
* @brief Get the blowfish encryption handler for a key
* @param $key string (optional)
* @param $key string (optional)
* @return \Crypt_Blowfish blowfish object
* @return \Crypt_Blowfish blowfish object
*
*
* if the key is left out, the default hande ler will be used
* if the key is left out, the default handler will be used
*/
*/
public static function getBlowfish($key = '') {
private static function getBlowfish($key = '') {
if ($key) {
if ($key) {
@ -596,38 +535,6 @@ class Crypt {
}
}
/**
* @param $passphrase
* @return mixed
*/
public static function legacyCreateKey($passphrase) {
// Generate a random integer
$key = mt_rand(10000, 99999) . mt_rand(10000, 99999) . mt_rand(10000, 99999) . mt_rand(10000, 99999);
// Encrypt the key with the passphrase
$legacyEncKey = self::legacyEncrypt($key, $passphrase);
return $legacyEncKey;
}
/**
* @brief encrypts content using legacy blowfish system
* @param string $content the cleartext message you want to encrypt
* @param string $passphrase
* @returns string encrypted content
*
* This function encrypts an content
*/
public static function legacyEncrypt($content, $passphrase = '') {
$bf = self::getBlowfish($passphrase);
return $bf->encrypt($content);
}
/**
/**
* @brief decrypts content using legacy blowfish system
* @brief decrypts content using legacy blowfish system
* @param string $content the cleartext message you want to decrypt
* @param string $content the cleartext message you want to decrypt
@ -665,4 +572,4 @@ class Crypt {
}
}
}
}
}
}