-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
56 lines (40 loc) · 1.28 KB
/
functions.php
File metadata and controls
56 lines (40 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
// Encryption functions
define('IV_SIZE', mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
function encrypt ($key, $value) {
$iv = mcrypt_create_iv(IV_SIZE, MCRYPT_DEV_URANDOM);
$crypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $value, MCRYPT_MODE_CBC, $iv);
$combo = $iv . $crypt;
$value = base64_encode($iv . $crypt);
return $value;
}
function decrypt ($key, $value) {
$combo = base64_decode($value);
$iv = substr($combo, 0, IV_SIZE);
$crypt = substr($combo, IV_SIZE, strlen($combo));
$value = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $crypt, MCRYPT_MODE_CBC, $iv);
return $value;
}
// Database functions
function saveToDB($db, $username, $message) {
if ($stmt = $db->prepare("INSERT INTO items (username, message) VALUES(?, ?)")) {
$stmt->bind_param("ss", $username, $message);
$stmt->execute();
return true;
} else {
return false;
}
}
function getFromDB($db, $username) {
if ($stmt = $db->prepare("SELECT message FROM items WHERE username=?")) {
$return = "";
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->bind_result($return);
$stmt->fetch();
$stmt->close();
return $return;
} else {
return false;
}
}