Link to Trubanc 1.0b1 announcement
[loomclient.git] / Cipher.php
blobb0ee303e0ddd4844558200f9efa522cff8721831
1 <?php
3 /*
4 * Some AES encryption. Works in PHP 4 or 5.
5 * Scrounged on the web, and made to work.
6 */
8 class Cipher {
9 var $securekey, $iv;
10 var $enc, $mode;
12 function Cipher($textkey) {
13 $this->enc = MCRYPT_RIJNDAEL_128;
14 $this->mode = MCRYPT_MODE_ECB;
15 $this->securekey = mhash(MHASH_SHA256,$textkey);
16 $iv_size = mcrypt_get_iv_size($this->enc, $this->mode);
17 $this->iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_RANDOM);
20 function encrypt($input) {
21 return mcrypt_encrypt($this->enc, $this->securekey, $input, $this->mode, $this->iv);
24 function decrypt($input) {
25 return trim(mcrypt_decrypt($this->enc, $this->securekey, $input, $this->mode, $this->iv));
28 function encrypt2hex($input) {
29 return bin2hex($this->encrypt($input));
32 function decrypthex($input) {
33 return $this->decrypt(pack("H*", $input));
37 /* test code
38 $cipher = new Cipher('secret passphrase');
40 $encryptedtext = $cipher->encrypt2hex(pack("H*", "1650f617c024d6441461b2538c6d9540"));
41 echo "->encrypt = $encryptedtext<br />";
43 $decryptedtext = bin2hex($cipher->decrypthex($encryptedtext));
44 echo "->decrypt = $decryptedtext<br />";
46 var_dump($cipher);
49 /* ***** BEGIN LICENSE BLOCK *****
50 * Version: MPL 1.1/GPL 2.0/LGPL 2.1/Apache 2.0
52 * The contents of this file are subject to the Mozilla Public License Version
53 * 1.1 (the "License"); you may not use this file except in compliance with
54 * the License. You may obtain a copy of the License at
55 * http://www.mozilla.org/MPL/
57 * Software distributed under the License is distributed on an "AS IS" basis,
58 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
59 * for the specific language governing rights and limitations under the
60 * License.
62 * The Original Code is LoomClient PHP library
64 * The Initial Developer of the Original Code is
65 * Bill St. Clair.
66 * Portions created by the Initial Developer are Copyright (C) 2008
67 * the Initial Developer. All Rights Reserved.
69 * Contributor(s):
70 * Bill St. Clair <bill@billstclair.com>
72 * Alternatively, the contents of this file may be used under the
73 * terms of the GNU General Public License Version 2 or later (the
74 * "GPL"), the GNU Lesser General Public License Version 2.1 or later
75 * (the "LGPL"), or The Apache License Version 2.0 (the "AL"), in
76 * which case the provisions of the GPL, LGPL, or AL are applicable
77 * instead of those above. If you wish to allow use of your version of
78 * this file only under the terms of the GPL, the LGPL, or the AL, and
79 * not to allow others to use your version of this file under the
80 * terms of the MPL, indicate your decision by deleting the provisions
81 * above and replace them with the notice and other provisions
82 * required by the GPL or the LGPL. If you do not delete the
83 * provisions above, a recipient may use your version of this file
84 * under the terms of any one of the MPL, the GPL the LGPL, or the AL.
85 ****** END LICENSE BLOCK ***** */