Encrypt.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * Encryption class.
  4. * Default mode is MYCRYPT__MODE_ECB.
  5. *
  6. * @author Ironpilot
  7. * @copyright Copywrite (c) 2011, STAPLE CODE
  8. *
  9. * This file is part of the STAPLE Framework.
  10. *
  11. * The STAPLE Framework is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by the
  13. * Free Software Foundation, either version 3 of the License, or (at your option)
  14. * any later version.
  15. *
  16. * The STAPLE Framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  18. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. class Staple_Encrypt
  25. {
  26. const AES = MCRYPT_RIJNDAEL_128;
  27. const ECB = MCRYPT_MODE_ECB;
  28. const CBC = MCRYPT_MODE_CBC;
  29. const CFB = MCRYPT_MODE_CFB;
  30. const OFB = MCRYPT_MODE_OFB;
  31. const NOFB = MCRYPT_MODE_NOFB;
  32. const STREAM = MCRYPT_MODE_STREAM;
  33. /**
  34. * The cypher to be used during encryption.
  35. * @var string
  36. */
  37. private $cypher;
  38. /**
  39. * Salt to added to/removed from the end of the encrypted value.
  40. * @var string
  41. */
  42. private $salt;
  43. /**
  44. * Pepper to be added to/removed from the beginning of the encrypted value.
  45. * @var string
  46. */
  47. private $pepper;
  48. /**
  49. * PHP MCRYPT mode. Should be one of the predefined mode constants for PHP. Defaults to MCRYPT_MODE_ECB.
  50. * @var string
  51. */
  52. private $mode = MCRYPT_MODE_ECB;
  53. /**
  54. * Encryption key
  55. * @var string
  56. */
  57. private $key;
  58. /**
  59. * Simple MD5 hashing function
  60. * @param string $encrypt
  61. * @return string
  62. */
  63. public static function MD5($hash)
  64. {
  65. return md5($hash);
  66. }
  67. /**
  68. * Simple SHA1 function
  69. * @param string $encrypt
  70. * @return string
  71. */
  72. public static function SHA1($hash)
  73. {
  74. return sha1($hash);
  75. }
  76. /**
  77. * Simple SHA256 Hasher
  78. * @param string $hash
  79. * @return string
  80. */
  81. public static function SHA256($hash)
  82. {
  83. return hash('sha256',$hash);
  84. }
  85. /**
  86. * @return the $cypher
  87. */
  88. public function getCypher()
  89. {
  90. return $this->cypher;
  91. }
  92. /**
  93. * @param string $cypher
  94. */
  95. public function setCypher($cypher)
  96. {
  97. $this->cypher = $cypher;
  98. return $this;
  99. }
  100. /**
  101. * @return the $salt
  102. */
  103. public function getSalt()
  104. {
  105. return $this->salt;
  106. }
  107. /**
  108. * @param string $salt
  109. */
  110. public function setSalt($salt)
  111. {
  112. $this->salt = $salt;
  113. return $this;
  114. }
  115. /**
  116. * @return the $pepper
  117. */
  118. public function getPepper()
  119. {
  120. return $this->pepper;
  121. }
  122. /**
  123. * @param string $pepper
  124. */
  125. public function setPepper($pepper)
  126. {
  127. $this->pepper = $pepper;
  128. return $this;
  129. }
  130. /**
  131. * @return the $mode
  132. */
  133. public function getMode()
  134. {
  135. return $this->mode;
  136. }
  137. /**
  138. * @param string $mode
  139. */
  140. public function setMode($mode)
  141. {
  142. $this->mode = $mode;
  143. return $this;
  144. }
  145. /**
  146. * @param string $key
  147. */
  148. public function setKey($key)
  149. {
  150. $this->key = $key;
  151. return $this;
  152. }
  153. /**
  154. * @todo Function is incomplete
  155. * Encrypt Data
  156. * @param string $text
  157. * @param string $key
  158. * @throws Exception
  159. * @return string
  160. */
  161. public static function Encrypt($text, $key = NULL)
  162. {
  163. if(isset($key))
  164. {
  165. $enckey = $key;
  166. }
  167. elseif(isset($this->key))
  168. {
  169. $enckey = $this->key;
  170. }
  171. else
  172. {
  173. throw new Exception('No Encryption Key', Staple_Error::APPLICATION_ERROR);
  174. }
  175. }
  176. /**
  177. * @todo Function is incomplete
  178. * Decrypt data
  179. * @param string $encryptedText
  180. * @param string $key
  181. * @throws Exception
  182. * @return string
  183. */
  184. public static function Decrypt($encryptedText, $key = NULL)
  185. {
  186. if(isset($key))
  187. {
  188. $enckey = $key;
  189. }
  190. elseif(isset($this->key))
  191. {
  192. $enckey = $this->key;
  193. }
  194. else
  195. {
  196. throw new Exception('No Decryption Key', Staple_Error::APPLICATION_ERROR);
  197. }
  198. }
  199. /**
  200. * Encrypt data with AES
  201. * @param string $encrypt
  202. * @param string $key
  203. * @param string $salt
  204. * @param string $pepper
  205. * @param string $iv
  206. * @return string
  207. */
  208. public static function AES_encrypt($encrypt, $key, $salt = '', $pepper = '', $iv = NULL)
  209. {
  210. if($iv == NULL)
  211. {
  212. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  213. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  214. }
  215. //Add salt and pepper
  216. $encstring = $pepper.$encrypt.$salt;
  217. return mcrypt_encrypt(self::AES, $key, $encstring, MCRYPT_MODE_ECB, $iv);
  218. }
  219. /**
  220. * Decrypt data using AES
  221. * @param string $decrypt
  222. * @param string $key
  223. * @param string $salt
  224. * @param string $pepper
  225. * @param string $iv
  226. * @return string
  227. */
  228. public static function AES_decrypt($decrypt, $key, $salt = '', $pepper = '', $iv = NULL)
  229. {
  230. if($iv == NULL)
  231. {
  232. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  233. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  234. }
  235. //To correctly detect string length we trim the output.
  236. $encstring = trim(mcrypt_decrypt(self::AES, $key, $decrypt, MCRYPT_MODE_ECB, $iv));
  237. //Remove salt and pepper
  238. $start = strlen($pepper);
  239. $end = strlen($encstring)-strlen($salt)-strlen($pepper);
  240. $encstring = substr($encstring, $start, $end);
  241. return $encstring;
  242. }
  243. /**
  244. * Generates a random hex value.
  245. * @param int $length
  246. * @return string
  247. */
  248. public static function genHex($length = 16)
  249. {
  250. $hex = '';
  251. for ($i = 0; $i<$length; $i++)
  252. {
  253. $rnd = dechex(rand(0, 16));
  254. $hex .= $rnd;
  255. }
  256. return $hex;
  257. }
  258. /**
  259. * Converts a string to a hex value
  260. * @param string $str
  261. * @return string
  262. */
  263. public static function hexstr($str)
  264. {
  265. $arr = unpack("H*",$str);
  266. return $arr[1];
  267. }
  268. /**
  269. * Converts a hexed string to a normal string
  270. * @param string $str
  271. */
  272. public static function strhex($str)
  273. {
  274. return pack("H*",$str);
  275. }
  276. }