summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/Mail.java
blob: 4a0bb5223af58ce41897d1ebef103e93c5b85141 (plain) (blame)
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
package org.openslx.bwlp.sat.mail;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

import org.openslx.bwlp.thrift.iface.UserInfo;

public class Mail {

	private static final MessageDigest md;

	static {
		MessageDigest tmp;
		try {
			tmp = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			tmp = null;
		}
		md = tmp;
	}

	public final String id;
	public final String userId;
	public final String message;

	public Mail(String id, String userId, String message) {
		this.id = id;
		this.userId = userId;
		this.message = message;
	}

	public Mail(UserInfo recipient, String message) {
		this(hash(recipient, message), recipient.userId, message);
	}

	private static String hash(UserInfo recipient, String message) {
		synchronized (md) {
			md.update(recipient.userId.getBytes(StandardCharsets.UTF_8));
			md.update(message.getBytes(StandardCharsets.UTF_8));
			return Base64.getEncoder().encodeToString(md.digest());
		}
	}

}