| Total Complexity | 62 |
| Total Lines | 342 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like EncryptionEO often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EncryptionEO, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class EncryptionEO implements EncryptionHandler{ |
||
| 15 | private $encryptionMaterials = NULL; |
||
| 16 | private $ks3client = NULL; |
||
| 17 | public function __construct($ks3client,$encryptionMaterials){ |
||
| 18 | $this->encryptionMaterials = $encryptionMaterials; |
||
| 19 | $this->ks3client = $ks3client; |
||
| 20 | } |
||
| 21 | public function putObjectByContentSecurely($args=array()){ |
||
| 22 | $sek = EncryptionUtil::genereateOnceUsedKey(); |
||
| 23 | $encryptedSek = EncryptionUtil::encodeCek($this->encryptionMaterials,$sek); |
||
| 24 | $content = $args["Content"]; |
||
| 25 | if(empty($content)) |
||
| 26 | throw new Ks3ClientException("please specifie Content in request args"); |
||
| 27 | $metaContentLength = EncryptionUtil::metaTextLength($args); |
||
| 28 | $plainTextLength = strlen($content); |
||
| 29 | if($metaContentLength > 0 && $metaContentLength < $plainTextLength){ |
||
| 30 | $plainTextLength = $metaContentLength; |
||
| 31 | } |
||
| 32 | if($plainTextLength > 0) |
||
| 33 | $args["UserMeta"]["x-kss-meta-x-kss-unencrypted-content-length"] = $plainTextLength; |
||
| 34 | else |
||
| 35 | throw new Ks3ClientException("unexpected content length ".$plainTextLength); |
||
| 36 | |||
| 37 | $content = substr($content, 0,$plainTextLength); |
||
| 38 | |||
| 39 | |||
| 40 | $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
||
| 41 | $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td),MCRYPT_RAND); |
||
| 42 | mcrypt_generic_init($td,$sek,$iv); |
||
| 43 | //对content进行pkcs5填充 |
||
| 44 | $content = EncryptionUtil::PKCS5Padding($content,mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC)); |
||
| 45 | $encrypted = mcrypt_generic($td,$content); |
||
| 46 | mcrypt_generic_deinit($td); |
||
| 47 | |||
| 48 | $args["ObjectMeta"]["Content-Length"] = strlen($encrypted); |
||
| 49 | $args["Content"] = $encrypted; |
||
| 50 | |||
| 51 | $args = EncryptionUtil::updateContentMD5Header($args); |
||
| 52 | |||
| 53 | //TODO |
||
| 54 | $matdesc = "{}"; |
||
| 55 | if(ENCRYPTPTION_STORAGE_MODE == "ObjectMetadata"){ |
||
|
|
|||
| 56 | $args["UserMeta"]["x-kss-meta-x-kss-key"] = base64_encode($encryptedSek); |
||
| 57 | $args["UserMeta"]["x-kss-meta-x-kss-iv"] = base64_encode($iv); |
||
| 58 | $args["UserMeta"]["x-kss-meta-x-kss-matdesc"] = $matdesc; |
||
| 59 | } |
||
| 60 | |||
| 61 | $result = $this->ks3client->putObjectByContent($args); |
||
| 62 | |||
| 63 | if(ENCRYPTPTION_STORAGE_MODE == "InstructionFile"){ |
||
| 64 | $req = EncryptionUtil::createInstructionFile($args["Bucket"],$args["Key"], |
||
| 65 | base64_encode($encryptedSek),base64_encode($iv),$matdesc); |
||
| 66 | $this->ks3client->putObjectByContent($req); |
||
| 67 | } |
||
| 68 | |||
| 69 | return $result; |
||
| 70 | } |
||
| 71 | public function putObjectByFileSecurely($args=array()){ |
||
| 72 | $sek = EncryptionUtil::genereateOnceUsedKey(); |
||
| 73 | $encryptedSek = EncryptionUtil::encodeCek($this->encryptionMaterials,$sek); |
||
| 74 | if(!isset($args["Content"])||!is_array($args["Content"]) |
||
| 75 | ||!isset($args["Content"]["content"]) |
||
| 76 | ||empty($args["Content"]["content"])) |
||
| 77 | throw new Ks3ClientException("please specifie file content in request args"); |
||
| 78 | $content = $args["Content"]; |
||
| 79 | $plainTextLength = EncryptionUtil::plainTextLength($args); |
||
| 80 | if($plainTextLength <= 0){ |
||
| 81 | throw new Ks3ClientException("get content length failed ,unexpected content length ".$plainTextLength); |
||
| 82 | } |
||
| 83 | $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
||
| 84 | $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td),MCRYPT_RAND); |
||
| 85 | |||
| 86 | $args = EncryptionUtil::updateContentMD5Header($args); |
||
| 87 | $encryptedLength = EncryptionUtil::getPKCS5EncrypedLength($plainTextLength,mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC)); |
||
| 88 | |||
| 89 | $args["ObjectMeta"]["Content-Length"] = $encryptedLength; |
||
| 90 | $args["UserMeta"]["x-kss-meta-x-kss-unencrypted-content-length"] = $plainTextLength; |
||
| 91 | |||
| 92 | $readCallBack = new AESCBCStreamReadCallBack(); |
||
| 93 | $readCallBack->iv = $iv; |
||
| 94 | $readCallBack->cek = $sek; |
||
| 95 | $readCallBack->contentLength = $plainTextLength; |
||
| 96 | $args["readCallBack"] = $readCallBack; |
||
| 97 | |||
| 98 | //TODO |
||
| 99 | $matdesc = "{}"; |
||
| 100 | if(ENCRYPTPTION_STORAGE_MODE == "ObjectMetadata"){ |
||
| 101 | $args["UserMeta"]["x-kss-meta-x-kss-key"] = base64_encode($encryptedSek); |
||
| 102 | $args["UserMeta"]["x-kss-meta-x-kss-iv"] = base64_encode($iv); |
||
| 103 | $args["UserMeta"]["x-kss-meta-x-kss-matdesc"] = $matdesc; |
||
| 104 | } |
||
| 105 | |||
| 106 | $result = $this->ks3client->putObjectByFile($args); |
||
| 107 | |||
| 108 | if(ENCRYPTPTION_STORAGE_MODE == "InstructionFile"){ |
||
| 109 | $req = EncryptionUtil::createInstructionFile($args["Bucket"],$args["Key"], |
||
| 110 | base64_encode($encryptedSek),base64_encode($iv),$matdesc); |
||
| 111 | $this->ks3client->putObjectByContent($req); |
||
| 112 | } |
||
| 113 | |||
| 114 | return $result; |
||
| 115 | } |
||
| 116 | public function getObjectSecurely($args=array()){ |
||
| 276 | } |
||
| 277 | } |
||
| 278 | public function initMultipartUploadSecurely($args=array()){ |
||
| 279 | $sek = EncryptionUtil::genereateOnceUsedKey(); |
||
| 280 | $encryptedSek = EncryptionUtil::encodeCek($this->encryptionMaterials,$sek); |
||
| 281 | $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
||
| 282 | $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td),MCRYPT_RAND); |
||
| 283 | |||
| 284 | $matdesc = "{}"; |
||
| 285 | if(ENCRYPTPTION_STORAGE_MODE == "ObjectMetadata"){ |
||
| 286 | $args["UserMeta"]["x-kss-meta-x-kss-key"] = base64_encode($encryptedSek); |
||
| 287 | $args["UserMeta"]["x-kss-meta-x-kss-iv"] = base64_encode($iv); |
||
| 288 | $args["UserMeta"]["x-kss-meta-x-kss-matdesc"] = $matdesc; |
||
| 289 | } |
||
| 290 | |||
| 291 | $initResult = $this->ks3client->initMultipartUpload($args); |
||
| 292 | |||
| 293 | EncryptionUtil::initMultipartUploadContext($initResult,$iv,$sek,$encryptedSek,$matdesc); |
||
| 294 | |||
| 295 | return $initResult; |
||
| 296 | } |
||
| 297 | public function uploadPartSecurely($args=array()){ |
||
| 298 | $uploadId = $args["Options"]["uploadId"]; |
||
| 299 | $isLastPart = FALSE; |
||
| 300 | $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC); |
||
| 301 | if(isset($args["LastPart"])) |
||
| 302 | $isLastPart = $args["LastPart"]; |
||
| 303 | $exists = EncryptionUtil::multipartUploadContextExists($uploadId); |
||
| 304 | if(!$exists){ |
||
| 305 | throw new Ks3ClientException("no such upload in cache/encryption/"); |
||
| 306 | } |
||
| 307 | $context = EncryptionUtil::getMultipartUploadContext($uploadId); |
||
| 308 | if($context["lastPart"]){ |
||
| 309 | throw new Ks3ClientException("this upload with uploadId ".$uploadId," has been upload last part"); |
||
| 310 | } |
||
| 311 | $plainTextLength = EncryptionUtil::plainTextLength($args); |
||
| 312 | if($plainTextLength <= 0){ |
||
| 313 | throw new Ks3ClientException("get content length failed ,unexpected content length ".$plainTextLength); |
||
| 314 | } |
||
| 315 | if(!$isLastPart){ |
||
| 316 | if($plainTextLength % $blocksize != 0) |
||
| 317 | throw new Ks3ClientException("Invalid part size,part size (".$plainTextLength.") must be multiples of the block size ".$blocksize); |
||
| 318 | }else{ |
||
| 319 | $args["ObjectMeta"]["Content-Length"] = $plainTextLength + ($blocksize - $plainTextLength%$blocksize); |
||
| 320 | } |
||
| 321 | $readCallBack = new AESCBCStreamReadCallBack(); |
||
| 322 | $readCallBack->iv = base64_decode($context["nextIv"]); |
||
| 323 | $readCallBack->cek = base64_decode($context["cek"]); |
||
| 324 | $readCallBack->contentLength = $plainTextLength; |
||
| 325 | $readCallBack->mutipartUpload = TRUE; |
||
| 326 | $readCallBack->isLastPart = $isLastPart; |
||
| 327 | $args["readCallBack"] = $readCallBack; |
||
| 328 | |||
| 329 | $upResult = $this->ks3client->uploadPart($args); |
||
| 330 | EncryptionUtil::updateMultipartUploadContext($uploadId,$readCallBack->iv,$isLastPart); |
||
| 331 | return $upResult; |
||
| 332 | } |
||
| 333 | public function abortMultipartUploadSecurely($args=array()){ |
||
| 334 | $uploadId = $args["Options"]["uploadId"]; |
||
| 335 | EncryptionUtil::deleteMultipartUploadContext($uploadId); |
||
| 336 | return $this->ks3client->abortMultipartUpload($args); |
||
| 337 | } |
||
| 338 | public function completeMultipartUploadSecurely($args=array()){ |
||
| 356 | } |
||
| 357 | } |
||
| 358 | ?> |
||