Skip to content

Commit a23b726

Browse files
committed
commented code
1 parent 9e5b634 commit a23b726

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

pages/sodiumtest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
<?php
2-
2+
//Generates cryptographically secure pseudo-random KEY from sodium library
33
$key = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES);
4+
//Generates cryptographically secure pseudo-random nonce from sodium library
45
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
6+
//Authenticated secret-key encryption
57
$ciphertext = sodium_crypto_secretbox("password", $nonce, $key);
8+
//Encode into 64
69
$encoded = base64_encode($nonce . $ciphertext);
710

811
echo $encoded . "<br>";
912
echo $key . "<br>";
1013

11-
14+
//Decode ciphertext from base64 to plain string
1215
$decoded = base64_decode($encoded);
1316
$nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
1417
$ciphertext = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');

pages/upload.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
session_start();
88
require_once "config.php";
9-
// Check if image file is a actual image or fake image
9+
// Check if image file is a actual image
1010
if (isset($_POST["submit"])) {
1111
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
1212
if ($check !== false) {
@@ -46,9 +46,6 @@
4646
} else {
4747
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
4848
//Write $target_file to db
49-
50-
51-
5249
//Prepare sql statement
5350
$sql = "UPDATE users SET profilepicture = ? WHERE id = ?";
5451

pages/uploadPassword.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55

66

77
require_once "config.php";
8-
//data to enter
98

9+
//SQL Statement
1010
$sql = "INSERT INTO passwordentrys (name, password, url, userid, username,keyy) VALUES (?, ?, ?, ? ,?,?)";
1111

12+
//Initialiaze validation variables
1213
$validation = true;
1314
$validationErrorText = "";
1415

16+
//Prepare statement
1517
if ($stmt = $mysqli->prepare($sql)) {
1618
// Bind variables to the prepared statement as parameters
1719
$stmt->bind_param("ssssss", $param_name, $param_password, $param_url, $param_userid, $param_username, $param_keyy);
1820

21+
22+
//Validate Data for length
1923
if (strlen($_POST["name"]) >= 45) {
2024
$validation = false;
2125
$validationErrorText .= "The name cannot be longer than 45 characters <br>";
@@ -33,6 +37,7 @@
3337
$validationErrorText .= "The username cannot be longer than 45 characters <br>";
3438
}
3539

40+
//Validate data for isset
3641
if (!(isset($_POST["name"]) && isset($_POST["password"]) && isset($_POST["url"]) && isset($_POST["username"]))) {
3742
$validation = false;
3843
$validationErrorText .= "Please make sure all fields are filled out! <br>";
@@ -44,7 +49,7 @@
4449
$ciphertext = sodium_crypto_secretbox($_POST["password"], $nonce, $key);
4550
$encoded = base64_encode($nonce . $ciphertext);
4651

47-
52+
//If validation passed then set parameters and execute
4853
if ($validation) {
4954
// Set parameters
5055
$param_name = htmlentities($_POST["name"]);
@@ -55,7 +60,7 @@
5560
$param_username = htmlentities($_POST["username"]);
5661

5762

58-
// Attempt to execute the prepared statement
63+
// Attempt to execute the prepared statement and redirect if successful
5964
if ($stmt->execute()) {
6065
header("location: passwordmanager.php");
6166
} else {
@@ -99,6 +104,10 @@
99104
<br>
100105
<br>
101106

107+
<!--
108+
Form to upload password
109+
includes client-side validation
110+
-->
102111
<form action="uploadPassword.php" method="post" enctype="multipart/form-data">
103112
<div class="container">
104113
<div class="row">

0 commit comments

Comments
 (0)