iud_key_imp10/Quellcodes/iud_key_rsachat/Nachricht.java
2024-12-29 14:26:34 +01:00

183 lines
4.3 KiB
Java

/**
*
* description
*
* @version 1.1 from 30.01.2020
* Änderungen: Hash-Funktion verbessert, damit ähnliche Texte nicht so ähnliche Hashes bekommen
* @author
*/
import java.util.ArrayList;
import java.security.MessageDigest;
import java.nio.charset.StandardCharsets;
public class Nachricht {
// Anfang Attribute
private byte[] inhalt;
private String absender;
private String empfaenger;
private ArrayList<Key> keys;
private ArrayList<Key> hashKeys;
private byte[] hashCode;
private long time;
private boolean stopped;
private boolean sended;
// Ende Attribute
public Nachricht(String absender, String empfaenger, String inhalt) {
setInhalt(inhalt);
this.absender = absender;
this.empfaenger = empfaenger;
this.keys = new ArrayList<Key>();
this.hashKeys = new ArrayList<Key>();
this.hashCode = ("").getBytes() ;
this.time = System.currentTimeMillis();
this.stopped = false;
this.sended = false;
}
public Nachricht(String absender, String empfaenger, byte[] inhalt, byte[] hashCode) {
this.inhalt = inhalt;
this.absender = absender;
this.empfaenger = empfaenger;
this.keys = new ArrayList<Key>();
this.hashKeys = new ArrayList<Key>();
this.hashCode = hashCode;
this.time = System.currentTimeMillis();
this.stopped = false;
this.sended = false;
}
public Nachricht(String absender, String empfaenger, String inhalt, ArrayList<Key> keys, ArrayList<Key> hashKeys) {
setInhalt(inhalt);
this.absender = absender;
this.empfaenger = empfaenger;
this.keys = (ArrayList<Key>)keys.clone();
this.hashKeys = (ArrayList<Key>) hashKeys.clone();
this.hashCode = ("").getBytes() ;
this.time = System.currentTimeMillis();
this.stopped = false;
this.sended = false;
}
public Nachricht(String absender, String empfaenger, byte[] inhalt, byte[] hashCode, ArrayList<Key> keys, ArrayList<Key> hashKeys) {
this.inhalt = inhalt;
this.absender = absender;
this.empfaenger = empfaenger;
this.keys = (ArrayList<Key>)keys.clone();
this.hashKeys = (ArrayList<Key>) hashKeys.clone();
this.hashCode = hashCode;
this.time = System.currentTimeMillis();
this.stopped = false;
this.sended = false;
}
// Anfang Methoden
public String getInhaltString() {
return new String(inhalt);
}
public byte[] getInhalt() {
return inhalt;
}
public void setInhalt(String inhalt) {
this.inhalt = inhalt.getBytes();
}
public void setInhalt(byte[] inhalt) {
this.inhalt = inhalt;
}
public String getAbsender() {
return absender;
}
public void setAbsender(String absender) {
this.absender = absender;
}
public String getEmpfaenger() {
return empfaenger;
}
public void setEmpfaenger(String empfaenger) {
this.empfaenger = empfaenger;
}
public Nachricht copy() {
Nachricht n = new Nachricht(absender, "", inhalt, hashCode, keys, hashKeys);
return n;
}
public void addKey(Key k) {
keys.add(k);
}
public ArrayList<Key> getKeys() {
return keys;
}
public void addHashKey(Key k) {
hashKeys.add(k);
}
public ArrayList<Key> getHashKeys() {
return hashKeys;
}
public void generateHash() {
try{
MessageDigest digest = MessageDigest.getInstance("SHA-256");
hashCode = Base64.encode(digest.digest(getInhaltString().getBytes(StandardCharsets.UTF_8))).substring(0,10).getBytes();
} catch(Exception e) {
System.out.println(e);
hashCode = new byte[0];
}
}
public byte[] getHashCode() {
return hashCode;
}
public void setHashCode(byte[] hashCode) {
this.hashCode = hashCode;
}
public int getRestzeit() {
if (sended || stopped) {
return 0;
} // end of if
return 10 - (int) ((System.currentTimeMillis()-this.time)/1000);
}
public boolean getSended() {
return sended;
}
public void setSended(boolean s) {
sended = s;
}
public boolean getStopped() {
return stopped;
}
public void setStopped(boolean s) {
stopped = s;
}
// Ende Methoden
}