5757 * @package OCA\Encryption\Crypto
5858 */
5959class Crypt {
60+ public const SUPPORTED_CIPHERS_AND_KEY_SIZE = [
61+ 'AES-256-CTR ' => 32 ,
62+ 'AES-128-CTR ' => 16 ,
63+ 'AES-256-CFB ' => 32 ,
64+ 'AES-128-CFB ' => 16 ,
65+ ];
66+ // one out of SUPPORTED_CIPHERS_AND_KEY_SIZE
6067 public const DEFAULT_CIPHER = 'AES-256-CTR ' ;
6168 // default cipher from old Nextcloud versions
6269 public const LEGACY_CIPHER = 'AES-128-CFB ' ;
6370
71+ public const SUPPORTED_KEY_FORMATS = ['hash ' , 'password ' ];
72+ // one out of SUPPORTED_KEY_FORMATS
73+ public const DEFAULT_KEY_FORMAT = 'hash ' ;
6474 // default key format, old Nextcloud version encrypted the private key directly
6575 // with the user password
6676 public const LEGACY_KEY_FORMAT = 'password ' ;
@@ -77,20 +87,9 @@ class Crypt {
7787 /** @var IConfig */
7888 private $ config ;
7989
80- /** @var array */
81- private $ supportedKeyFormats ;
82-
8390 /** @var IL10N */
8491 private $ l ;
8592
86- /** @var array */
87- private $ supportedCiphersAndKeySize = [
88- 'AES-256-CTR ' => 32 ,
89- 'AES-128-CTR ' => 16 ,
90- 'AES-256-CFB ' => 32 ,
91- 'AES-128-CFB ' => 16 ,
92- ];
93-
9493 /** @var bool */
9594 private $ supportLegacy ;
9695
@@ -105,8 +104,6 @@ public function __construct(ILogger $logger, IUserSession $userSession, IConfig
105104 $ this ->user = $ userSession && $ userSession ->isLoggedIn () ? $ userSession ->getUser ()->getUID () : '"no user given" ' ;
106105 $ this ->config = $ config ;
107106 $ this ->l = $ l ;
108- $ this ->supportedKeyFormats = ['hash ' , 'password ' ];
109-
110107 $ this ->supportLegacy = $ this ->config ->getSystemValueBool ('encryption.legacy_format_support ' , false );
111108 }
112109
@@ -207,12 +204,12 @@ public function symmetricEncryptFileContent($plainContent, $passPhrase, $version
207204 /**
208205 * generate header for encrypted file
209206 *
210- * @param string $keyFormat (can be 'hash' or 'password')
207+ * @param string $keyFormat see SUPPORTED_KEY_FORMATS
211208 * @return string
212209 * @throws \InvalidArgumentException
213210 */
214- public function generateHeader ($ keyFormat = ' hash ' ) {
215- if (in_array ($ keyFormat , $ this -> supportedKeyFormats , true ) === false ) {
211+ public function generateHeader ($ keyFormat = self :: DEFAULT_KEY_FORMAT ) {
212+ if (in_array ($ keyFormat , self :: SUPPORTED_KEY_FORMATS , true ) === false ) {
216213 throw new \InvalidArgumentException ('key format " ' . $ keyFormat . '" is not supported ' );
217214 }
218215
@@ -259,14 +256,15 @@ private function encrypt($plainContent, $iv, $passPhrase = '', $cipher = self::D
259256 */
260257 public function getCipher () {
261258 $ cipher = $ this ->config ->getSystemValue ('cipher ' , self ::DEFAULT_CIPHER );
262- if (!isset ($ this -> supportedCiphersAndKeySize [$ cipher ])) {
259+ if (!isset (self :: SUPPORTED_CIPHERS_AND_KEY_SIZE [$ cipher ])) {
263260 $ this ->logger ->warning (
264- sprintf (
265- 'Unsupported cipher (%s) defined in config.php supported. Falling back to %s ' ,
266- $ cipher ,
267- self ::DEFAULT_CIPHER
268- ),
269- ['app ' => 'encryption ' ]);
261+ sprintf (
262+ 'Unsupported cipher (%s) defined in config.php supported. Falling back to %s ' ,
263+ $ cipher ,
264+ self ::DEFAULT_CIPHER
265+ ),
266+ ['app ' => 'encryption ' ]
267+ );
270268 $ cipher = self ::DEFAULT_CIPHER ;
271269 }
272270
@@ -288,8 +286,8 @@ public function getCipher() {
288286 * @throws \InvalidArgumentException
289287 */
290288 protected function getKeySize ($ cipher ) {
291- if (isset ($ this -> supportedCiphersAndKeySize [$ cipher ])) {
292- return $ this -> supportedCiphersAndKeySize [$ cipher ];
289+ if (isset (self :: SUPPORTED_CIPHERS_AND_KEY_SIZE [$ cipher ])) {
290+ return self :: SUPPORTED_CIPHERS_AND_KEY_SIZE [$ cipher ];
293291 }
294292
295293 throw new \InvalidArgumentException (
@@ -411,7 +409,7 @@ public function decryptPrivateKey($privateKey, $password = '', $uid = '') {
411409 $ keyFormat = self ::LEGACY_KEY_FORMAT ;
412410 }
413411
414- if ($ keyFormat === ' hash ' ) {
412+ if ($ keyFormat === self :: DEFAULT_KEY_FORMAT ) {
415413 $ password = $ this ->generatePasswordHash ($ password , $ cipher , $ uid );
416414 }
417415
0 commit comments