1 package beecrypt.crypto.spec;
2 
3 import java.security.spec.*;
4 import java.util.regex.*;
5 
6 public class DHIESParameterSpec implements AlgorithmParameterSpec {
7 	private static final Pattern DHIESPAT = Pattern
8 			.compile("DHIES\\((\\w(?:\\w|\\d)*(?:-(?:\\w|\\d)*)*),(\\w(?:\\w|\\d)*(?:-(?:\\w|\\d)*)*),(\\w(?:\\w|\\d)*(?:-(?:\\w|\\d)*)*)(?:,(\\d+))?(?:,(\\d+))?\\)");
9 
10 	private String messageDigestAlgorithm;
11 	private String cipherAlgorithm;
12 	private String macAlgorithm;
13 
14 	private int cipherKeyLength;
15 	private int macKeyLength;
16 
DHIESParameterSpec(String description)17 	public DHIESParameterSpec(String description)
18 			throws IllegalArgumentException {
19 		Matcher m = DHIESPAT.matcher(description);
20 		if (!m.matches())
21 			throw new IllegalArgumentException(
22 					"couldn't parse descriptor into DHIES(<digest>,<cipher>,<mac>[,<cipherkeylen>[,<mackeylen>]])");
23 
24 		messageDigestAlgorithm = m.group(1);
25 		cipherAlgorithm = m.group(2);
26 		macAlgorithm = m.group(3);
27 
28 		String tmp = m.group(4);
29 		if (tmp != null && tmp.length() > 0)
30 			cipherKeyLength = Integer.parseInt(tmp);
31 		else
32 			cipherKeyLength = 0;
33 
34 		tmp = m.group(5);
35 		if (tmp != null && tmp.length() > 0)
36 			macKeyLength = Integer.parseInt(tmp);
37 		else
38 			macKeyLength = 0;
39 	}
40 
DHIESParameterSpec(String messageDigestAlgorithm, String cipherAlgorithm, String macAlgorithm)41 	public DHIESParameterSpec(String messageDigestAlgorithm,
42 			String cipherAlgorithm, String macAlgorithm) {
43 		this(messageDigestAlgorithm, cipherAlgorithm, macAlgorithm, 0, 0);
44 	}
45 
DHIESParameterSpec(String messageDigestAlgorithm, String cipherAlgorithm, String macAlgorithm, int cipherKeyLength, int macKeyLength)46 	public DHIESParameterSpec(String messageDigestAlgorithm,
47 			String cipherAlgorithm, String macAlgorithm, int cipherKeyLength,
48 			int macKeyLength) {
49 		if (cipherKeyLength < 0 || macKeyLength < 0)
50 			throw new IllegalArgumentException();
51 
52 		this.messageDigestAlgorithm = messageDigestAlgorithm;
53 		this.cipherAlgorithm = cipherAlgorithm;
54 		this.macAlgorithm = macAlgorithm;
55 
56 		this.cipherKeyLength = cipherKeyLength;
57 		this.macKeyLength = macKeyLength;
58 	}
59 
DHIESParameterSpec(DHIESParameterSpec copy)60 	public DHIESParameterSpec(DHIESParameterSpec copy) {
61 		this.messageDigestAlgorithm = copy.messageDigestAlgorithm;
62 		this.cipherAlgorithm = copy.cipherAlgorithm;
63 		this.macAlgorithm = copy.macAlgorithm;
64 
65 		this.cipherKeyLength = copy.cipherKeyLength;
66 		this.macKeyLength = copy.macKeyLength;
67 	}
68 
getCipherAlgorithm()69 	public String getCipherAlgorithm() {
70 		return cipherAlgorithm;
71 	}
72 
getMacAlgorithm()73 	public String getMacAlgorithm() {
74 		return macAlgorithm;
75 	}
76 
getMessageDigestAlgorithm()77 	public String getMessageDigestAlgorithm() {
78 		return messageDigestAlgorithm;
79 	}
80 
getCipherKeyLength()81 	public int getCipherKeyLength() {
82 		return cipherKeyLength;
83 	}
84 
getMacKeyLength()85 	public int getMacKeyLength() {
86 		return macKeyLength;
87 	}
88 
toString()89 	public String toString() {
90 		StringBuffer tmp = new StringBuffer();
91 
92 		tmp.append("DHIES(").append(messageDigestAlgorithm).append(',').append(
93 				cipherAlgorithm).append(',').append(macAlgorithm);
94 
95 		if (macKeyLength > 0) {
96 			tmp.append(',').append(Integer.toString(cipherKeyLength));
97 			tmp.append(',').append(Integer.toString(macKeyLength));
98 		} else if (cipherKeyLength > 0) {
99 			tmp.append(',').append(Integer.toString(cipherKeyLength));
100 		}
101 
102 		return tmp.append(')').toString();
103 	}
104 }
105