Commit 6e921ea4 authored by Oskars's avatar Oskars
Browse files

Add: Classes - Post, Page, Encryption, Verification, GuestUSer, USer

parent bb15de1e
package model;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Encryption {
public static String encryptMD5(String inputPassword) {
String generatedPassword = null;
try{
MessageDigest md = MessageDigest.getInstance("MD5");// Create MessageDigest instance for MD5
md.update( inputPassword.getBytes() );// Add password bytes to digest
byte[] bytes = md.digest(); // Get the hash's bytes // returns the array of bytes in decimal format
StringBuilder sb = new StringBuilder(); // Convert byte[] to hexadecimal // mutable string of characters - can be changed
for( int i = 0; i < bytes.length; i++) {
// converting byte values to integer then to string
// not quite sure about this
sb.append( Integer.toString( ( bytes[i] & 0xff ) + 0x100, 16 ).substring(1) );
}
generatedPassword = sb.toString();
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}
return generatedPassword;
}
}
package model;
import java.util.ArrayList;
public class Page {
private String title;
private String description;
private ArrayList<User> followers; // Need to make class Users
private ArrayList<Post> postsInPage;
//CONSTRUCTORS
public Page() {
title = "Empty";
description = "Empty";
followers = new ArrayList<User>();
postsInPage = new ArrayList<Post>();
}
//SETTERS
public void setTitle(String title) {
this.title = (title != null) ? title : "no title";
}
public void setDescription(String description) {
this.description = (description != null) ? description : "no description";
}
public void setFollowers(ArrayList<User> followers) {
if(followers != null && !followers.isEmpty() ) this.followers = followers;
}
public void setPostsInPage(ArrayList<Post> posts) {
if(posts != null && !posts.isEmpty() ) postsInPage = posts;
}
//GETTERS
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public ArrayList<User> getFollowers() {
return followers;
}
public ArrayList<Post> getPostsInPage() {
return postsInPage;
}
//OTHER METHODS
public String toString() {
return "Title: " + title + ", Description: " + description + ", Followers " + followers.size() + ", Posts: " + postsInPage.size();
}
}
package model;
import java.time.LocalDate;
public class Post {
private String msg;
private LocalDate date;
private int countOfLikes;
//CONSTRUCTORS
public Post(){
msg = "Empty";
date = LocalDate.now();
countOfLikes = 0;
}
public Post(String msg) {
this.msg = (msg != null) ? msg : "";
}
//SETTERS
public void setMsg(String msg) {
this.msg = (msg != null)? msg : "";
}
//GETTERS
public String getMsg(){
return msg;
}
//OTHER METHODS
public String toString() {
return "Message: " + msg + ", Date: " + date.toString() + ", Likes: " + countOfLikes;
}
}
package model;
public class Verification {
public static boolean verificateNameAndSurname(String fullName) {
if(fullName == null || fullName.length() > 50) return false;
return fullName.matches( "([A-Z][a-z]*)(( )[A-Z][a-z]*)*)" )? true : false; // regex - First Last Etc Etc
}
public static boolean verificateUsername (String username) {
return (username != null && username.length() < 50)? true: false;
}
public static boolean verificatePassword(String password) {
return (password != null && password.length() < 50)? true : false;
}
}
package model.users;
public class GuestUser {
private int generatedId = 0;
private static int IdCounter = 0;
//CONSTRUCTOR
public GuestUser() {
generatedId = ++IdCounter;
}
//SETTERS
//GETTERS
}
package model.users;
import model.Encryption;
import model.Verification;
public class User {
private String nameAndSurname;
private String username;
private String password;
private String generatedPassword;
//MD5 hashes are no longer considered cryptographically secure, and they should not be used for cryptographic authentication.
//CONSTRUCTORS
public User() {
nameAndSurname = "Empty";
username = "Empty";
password = "Empty";
generatedPassword = null;
}
public User(String nameAndSurname, String username, String password) {
this.nameAndSurname = (Verification.verificateNameAndSurname(nameAndSurname))
? nameAndSurname : "Empty";
this.username = (Verification.verificateUsername(username))
? username : "Empty";
if(Verification.verificatePassword(password)) {
this.password = password;
generatedPassword = Encryption.encryptMD5(password);
}else {
this.password = "Empty";
generatedPassword = null;
}
}
//SETTERS
public void setNameAndSurname(String nameAndSurname) {
this.nameAndSurname = (Verification.verificateNameAndSurname(nameAndSurname)) ? nameAndSurname : "Empty";
}
public void setUsername(String username) {
this.username = (Verification.verificateUsername(username))
? username : "Empty";
}
public void setPassword(String password) {
if(Verification.verificatePassword(password)) {
this.password = password;
generatedPassword = Encryption.encryptMD5(password);
}else {
this.password = "Empty";
generatedPassword = null;
}
}
//GETTERS
public String getNameAndSurname() {
return nameAndSurname;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getGeneratedPassword() {
return generatedPassword;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment