-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPublicKey.java
More file actions
134 lines (123 loc) · 3.13 KB
/
Copy pathPublicKey.java
File metadata and controls
134 lines (123 loc) · 3.13 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package sysmlinjava.valuetypes;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import sysmlinjava.annotations.Attribute;
import sysmlinjava.units.SysMLinJavaUnits;
/**
* SysMLinJava value type for a public encryption key
*
* @author ModelerOne
*/
public class PublicKey extends SysMLValueType
{
/**
* Attribute for the value of the key as a byte array
*/
@Attribute
public byte[] value;
/**
* Map of the string/character value to the byte value
*/
final Map<String, Byte> stringByteMap;
/**
* Constant value of the hexadecimal characters
*/
static final char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
/**
* Constructor - initialize attributes
*/
public PublicKey()
{
super();
value = new byte[0];
stringByteMap = new HashMap<>();
for (int i = 0; i < hexChars.length; i++)
for (int j = 0; j < hexChars.length; j++)
{
char[] chars = {hexChars[i], hexChars[j]};
byte rightNib = (byte)j;
byte leftNib = (byte)i;
byte byteValue = (byte)((leftNib << 4) | rightNib);
stringByteMap.put(String.valueOf(chars), byteValue);
}
}
/**
* Constructor
*
* @param value byte array value to ve used as the key initial value
*/
public PublicKey(byte[] value)
{
this();
this.value = value;
}
/**
* Returns new instance with value from the specified string version of the key
*
* @param string string to be converted into the key
* @return new instance with value from the specified string version of the key
*/
public static PublicKey valueOf(String string)
{
PublicKey result = new PublicKey();
if (!string.isBlank() && string.length() > 20)
result.fromHexString(string);
return result;
}
/**
* Returns whether the key value is present in this instance
*
* @return whether the key value is present
*/
public boolean isPresent()
{
return value != null && value.length > 10;
}
@Override
protected void createUnits()
{
units = SysMLinJavaUnits.Bytes;
}
/**
* Returns a string representation of the key
*
* @return string representation of the key
*/
public String toHexString()
{
StringBuilder bldr = new StringBuilder();
for (int i = 0; i < value.length; i++)
{
int leftNib = value[i] >> 4 & 0x0F;
int rightNib = value[i] & 0x0F;
bldr.append(hexChars[leftNib]);
bldr.append(hexChars[rightNib]);
}
return bldr.toString();
}
/**
* Sets this key from a conversion of the specified hex string value
*
* @param hexString hex string representation of the key
*/
public void fromHexString(String hexString)
{
int numBytes = hexString.length() / 2;
value = new byte[numBytes];
for (int i = 0; i < numBytes; i++)
{
int j = i * 2;
value[i] = stringByteMap.get(hexString.substring(j, j + 2));
}
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append("PublicKey [value=");
builder.append(Arrays.toString(value));
builder.append("]");
return builder.toString();
}
}