Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/westberliner/checksum.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPatrick <patrick@westberliner.net>2017-01-11 14:18:19 +0300
committerPatrick <patrick@westberliner.net>2017-01-11 14:18:19 +0300
commit4fb77d526fdb40ce1756d82edec8b0229d434a3f (patch)
tree8d1f197a7dcbff38fdb55424aa4f21e605400561 /lib
parentcb6db71f83799aae313dc10c62a8c20fe7d4ef37 (diff)
adding hash algorithm selection before generating
Diffstat (limited to 'lib')
-rw-r--r--lib/Controller/ChecksumController.php36
1 files changed, 29 insertions, 7 deletions
diff --git a/lib/Controller/ChecksumController.php b/lib/Controller/ChecksumController.php
index 34e308e..c2a3dc2 100644
--- a/lib/Controller/ChecksumController.php
+++ b/lib/Controller/ChecksumController.php
@@ -23,15 +23,24 @@ class ChecksumController extends Controller {
/**
* callback function to get md5 hash of a file
* @param (string) $source - filename
- * @param (string) $dir - folder to file
+ * @param (string) $type - hash algorithm type
*/
- public function check($source) {
-
- if($md5 = $this->getHash($source)){
+ public function check($source, $type) {
+
+ if(!$this->checkAlgorithmType($type)) {
+ return new JSONResponse(
+ array(
+ 'response' => 'error',
+ 'msg' => $this->language->t('This is not a valid algorithm type.')
+ )
+ );
+ }
+
+ if($hash = $this->getHash($source, $type)){
return new JSONResponse(
array(
'response' => 'success',
- 'msg' => $md5
+ 'msg' => $hash
)
);
} else {
@@ -45,14 +54,27 @@ class ChecksumController extends Controller {
}
- protected function getHash($source) {
+ protected function getHash($source, $type) {
if($info = Filesystem::getLocalFile($source)) {
- return md5_file($info);
+ return hash_file($type, $info);
}
return false;
}
+ protected function checkAlgorithmType($type) {
+ return in_array($type, $this->getAllowedAlgorithmTypes());
+ }
+
+ protected function getAllowedAlgorithmTypes() {
+ return array(
+ 'md5',
+ 'sha1',
+ 'sha256',
+ 'sha512',
+ 'crc32'
+ );
+ }
}