remove old readme
[atutor.git] / mods / _standard / social / lib / Crypto.php
1 <?php
2 /**
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 class GeneralSecurityException extends Exception {
22 }
23
24 final class Crypto {
25   
26   /**
27    * HMAC algorithm to use
28    */
29   private static $HMAC_TYPE = "HMACSHA1";
30   
31   /** 
32    * minimum safe length for hmac keys (this is good practice, but not 
33    * actually a requirement of the algorithm
34    */
35   private static $MIN_HMAC_KEY_LEN = 8;
36   
37   /**
38    * Encryption algorithm to use
39    */
40   private static $CIPHER_TYPE = "AES/CBC/PKCS5Padding";
41   
42   private static $CIPHER_KEY_TYPE = "AES";
43   
44   /**
45    * Use keys of this length for encryption operations
46    */
47   public static $CIPHER_KEY_LEN = 16;
48   
49   private static $CIPHER_BLOCK_SIZE = 16;
50   
51   /**
52    * Length of HMAC SHA1 output
53    */
54   public static $HMAC_SHA1_LEN = 20;
55
56   private function __construct() {}
57
58   public static function hmacSha1Verify($key, $in, $expected) {
59     $hmac = Crypto::hmacSha1($key, $in);
60     if ($hmac != $expected) {
61       throw new GeneralSecurityException("HMAC verification failure");
62     }
63   }
64
65   public static function aes128cbcEncrypt($key, $text) {
66     /* Open the cipher */
67     $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
68     if (! $td) {
69       throw new GeneralSecurityException('Invalid mcrypt cipher, check your libmcrypt library and php-mcrypt extention');
70     }
71     // replaced MCRYPT_DEV_RANDOM with MCRYPT_RAND since windows doesn't have /dev/rand :)
72     srand((double)microtime() * 1000000);
73     $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
74     /* Intialize encryption */
75     mcrypt_generic_init($td, $key, $iv);
76     /* Encrypt data */
77     $encrypted = mcrypt_generic($td, $text);
78     /* Terminate encryption handler */
79     mcrypt_generic_deinit($td);
80     /*
81                  *  AES-128-CBC encryption.  The IV is returned as the first 16 bytes
82                  * of the cipher text.
83                  */
84     return $iv . $encrypted;
85   }
86
87   public static function aes128cbcDecrypt($key, $encrypted_text) {
88     /* Open the cipher */
89     $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
90     if (is_callable('mb_substr')) {
91       $iv = mb_substr($encrypted_text, 0, Crypto::$CIPHER_BLOCK_SIZE, 'latin1');
92     } else {
93       $iv = substr($encrypted_text, 0, Crypto::$CIPHER_BLOCK_SIZE);
94     }
95     /* Initialize encryption module for decryption */
96     mcrypt_generic_init($td, $key, $iv);
97     /* Decrypt encrypted string */
98     if (is_callable('mb_substr')) {
99       $encrypted = mb_substr($encrypted_text, Crypto::$CIPHER_BLOCK_SIZE, mb_strlen($encrypted_text, 'latin1'), 'latin1');
100     } else {
101       $encrypted = substr($encrypted_text, Crypto::$CIPHER_BLOCK_SIZE);
102     }
103     $decrypted = mdecrypt_generic($td, $encrypted);
104     /* Terminate decryption handle and close module */
105     mcrypt_generic_deinit($td);
106     mcrypt_module_close($td);
107     /* Show string */
108     return trim($decrypted);
109   }
110
111   public static function hmacSha1($key, $data) {
112     $blocksize = 64;
113     $hashfunc = 'sha1';
114     if (strlen($key) > $blocksize) {
115       $key = pack('H*', $hashfunc($key));
116     }
117     $key = str_pad($key, $blocksize, chr(0x00));
118     $ipad = str_repeat(chr(0x36), $blocksize);
119     $opad = str_repeat(chr(0x5c), $blocksize);
120     $hmac = pack('H*', $hashfunc(($key ^ $opad) . pack('H*', $hashfunc(($key ^ $ipad) . $data))));
121     return $hmac;
122   }
123 }