summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/mail/Mail.java
blob: 1d232a57c093e285a2814eb8ba6d252e8c329934 (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 org.apache.commons.codec.binary.Hex;
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 Hex.encodeHexString(md.digest());
		}
	}

}