本文整理汇总了Java中org.eclipse.leshan.util.Hex类的典型用法代码示例。如果您正苦于以下问题:Java Hex类的具体用法?Java Hex怎么用?Java Hex使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Hex类属于org.eclipse.leshan.util包,在下文中一共展示了Hex类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkRpkIdentity
import org.eclipse.leshan.util.Hex; //导入依赖的package包/类
private static boolean checkRpkIdentity(String endpoint, Identity clientIdentity, SecurityInfo securityInfo) {
// Manage RPK authentication
// ----------------------------------------------------
PublicKey publicKey = clientIdentity.getRawPublicKey();
if (LOG.isDebugEnabled()) {
LOG.debug("Registration request received using the secure endpoint with rpk '{}'",
Hex.encodeHexString(publicKey.getEncoded()));
}
if (publicKey == null || !publicKey.equals(securityInfo.getRawPublicKey())) {
if (LOG.isWarnEnabled()) {
LOG.warn("Invalid rpk for client {}: expected \n'{}'\n but was \n'{}'", endpoint,
Hex.encodeHexString(securityInfo.getRawPublicKey().getEncoded()),
Hex.encodeHexString(publicKey.getEncoded()));
}
return false;
} else {
LOG.debug("authenticated client '{}' using DTLS RPK", endpoint);
return true;
}
}
示例2: security_info_rpk_ser_des_then_equal
import org.eclipse.leshan.util.Hex; //导入依赖的package包/类
@Test
public void security_info_rpk_ser_des_then_equal() throws Exception {
byte[] publicX = Hex
.decodeHex("89c048261979208666f2bfb188be1968fc9021c416ce12828c06f4e314c167b5".toCharArray());
byte[] publicY = Hex
.decodeHex("cbf1eb7587f08e01688d9ada4be859137ca49f79394bad9179326b3090967b68".toCharArray());
// Get Elliptic Curve Parameter spec for secp256r1
AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC");
algoParameters.init(new ECGenParameterSpec("secp256r1"));
ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class);
// Create key specs
KeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(publicX), new BigInteger(publicY)),
parameterSpec);
SecurityInfo si = SecurityInfo.newRawPublicKeyInfo("myendpoint",
KeyFactory.getInstance("EC").generatePublic(publicKeySpec));
byte[] data = SecurityInfoSerDes.serialize(si);
assertEquals(
"{\"ep\":\"myendpoint\",\"rpk\":{\"x\":\"89c048261979208666f2bfb188be1968fc9021c416ce12828c06f4e314c167b5\",\"y\":\"cbf1eb7587f08e01688d9ada4be859137ca49f79394bad9179326b3090967b68\",\"params\":\"secp256r1\"}}",
new String(data));
System.err.println(new String(SecurityInfoSerDes.serialize(SecurityInfoSerDes.deserialize(data))));
assertEquals(si, SecurityInfoSerDes.deserialize(data));
}
示例3: decodeCoapResponse
import org.eclipse.leshan.util.Hex; //导入依赖的package包/类
private LwM2mNode decodeCoapResponse(LwM2mPath path, Response coapResponse, LwM2mRequest<?> request,
String endpoint) {
// Get content format
ContentFormat contentFormat = null;
if (coapResponse.getOptions().hasContentFormat()) {
contentFormat = ContentFormat.fromCode(coapResponse.getOptions().getContentFormat());
}
// Decode payload
try {
return decoder.decode(coapResponse.getPayload(), contentFormat, path, model);
} catch (CodecException e) {
if (LOG.isDebugEnabled()) {
byte[] payload = coapResponse.getPayload() == null ? new byte[0] : coapResponse.getPayload();
LOG.debug(
String.format("Unable to decode response payload of request [%s] from client [%s] [payload:%s]",
request, endpoint, Hex.encodeHexString(payload)));
}
throw new InvalidResponseException(e, "Unable to decode response payload of request [%s] from client [%s]",
request, endpoint);
}
}
示例4: checkRpkIdentity
import org.eclipse.leshan.util.Hex; //导入依赖的package包/类
private static boolean checkRpkIdentity(String endpoint, Identity clientIdentity, SecurityInfo securityInfo) {
// Manage RPK authentication
// ----------------------------------------------------
PublicKey publicKey = clientIdentity.getRawPublicKey();
if (publicKey == null || !publicKey.equals(securityInfo.getRawPublicKey())) {
if (LOG.isDebugEnabled()) {
LOG.debug("Invalid rpk for client {}: expected \n'{}'\n but was \n'{}'", endpoint,
Hex.encodeHexString(securityInfo.getRawPublicKey().getEncoded()),
Hex.encodeHexString(publicKey.getEncoded()));
}
return false;
} else {
LOG.trace("authenticated client '{}' using DTLS RPK", endpoint);
return true;
}
}
示例5: parseTlvValue
import org.eclipse.leshan.util.Hex; //导入依赖的package包/类
private static Object parseTlvValue(byte[] value, Type expectedType, LwM2mPath path) throws CodecException {
try {
LOG.trace("TLV value for path {} and expected type {}: {}", path, expectedType, value);
switch (expectedType) {
case STRING:
return TlvDecoder.decodeString(value);
case INTEGER:
return TlvDecoder.decodeInteger(value).longValue();
case FLOAT:
return TlvDecoder.decodeFloat(value).doubleValue();
case BOOLEAN:
return TlvDecoder.decodeBoolean(value);
case TIME:
return TlvDecoder.decodeDate(value);
case OPAQUE:
return value;
case OBJLNK:
return TlvDecoder.decodeObjlnk(value);
default:
throw new CodecException("Unsupported type %s for path %s", expectedType, path);
}
} catch (TlvException e) {
throw new CodecException(e, "Invalid content [%s] for type %s for path %s", Hex.encodeHexString(value),
expectedType, path);
}
}
示例6: decode_broken_tlv
import org.eclipse.leshan.util.Hex; //导入依赖的package包/类
@Test
public void decode_broken_tlv() throws TlvException {
String dataStr = "0011223344556677889900";
byte[] bytes = Hex.decodeHex(dataStr.toCharArray());
ByteBuffer b = ByteBuffer.wrap(bytes);
try {
TlvDecoder.decode(b);
fail();
} catch (TlvException ex) {
// this is very weak assertion since the format of the exception's message could
// be changed any time
// TODO: replace with more robust assertion or simply check for TlvException being thrown
assertEquals("Impossible to parse TLV: \n0011223344556677889900", ex.getMessage());
}
}
示例7: serialize
import org.eclipse.leshan.util.Hex; //导入依赖的package包/类
public static byte[] serialize(Observation obs) {
JsonObject o = Json.object();
o.set("request", Hex.encodeHexString(serializer.serializeRequest(obs.getRequest()).bytes));
if (obs.getContext() != null)
o.set("peer", EndpointContextSerDes.serialize(obs.getContext()));
else
o.set("peer", EndpointContextSerDes.serialize(obs.getRequest().getDestinationContext()));
if (obs.getRequest().getUserContext() != null) {
JsonObject ctxObject = Json.object();
for (Entry<String, String> e : obs.getRequest().getUserContext().entrySet()) {
ctxObject.set(e.getKey(), e.getValue());
}
o.set("context", ctxObject);
}
return o.toString().getBytes();
}
示例8: deserialize
import org.eclipse.leshan.util.Hex; //导入依赖的package包/类
public static Observation deserialize(byte[] data) {
JsonObject v = (JsonObject) Json.parse(new String(data));
EndpointContext endpointContext = EndpointContextSerDes.deserialize(v.get("peer").asObject());
byte[] req = Hex.decodeHex(v.getString("request", null).toCharArray());
RawData rawData = RawData.outbound(req, endpointContext, null, false);
Request request = (Request) parser.parseMessage(rawData);
request.setDestinationContext(endpointContext);
JsonValue ctxValue = v.get("context");
if (ctxValue != null) {
Map<String, String> context = new HashMap<>();
JsonObject ctxObject = (JsonObject) ctxValue;
for (String name : ctxObject.names()) {
context.put(name, ctxObject.getString(name, null));
}
request.setUserContext(context);
}
return new Observation(request, endpointContext);
}
示例9: serialize
import org.eclipse.leshan.util.Hex; //导入依赖的package包/类
public static JsonObject serialize(EndpointContext context) {
JsonObject peer = Json.object();
peer.set(KEY_ADDRESS, context.getPeerAddress().getHostString());
peer.set(KEY_PORT, context.getPeerAddress().getPort());
Principal principal = context.getPeerIdentity();
if (principal != null) {
if (principal instanceof PreSharedKeyIdentity) {
peer.set(KEY_ID, principal.getName());
} else if (principal instanceof RawPublicKeyIdentity) {
PublicKey publicKey = ((RawPublicKeyIdentity) principal).getKey();
peer.set(KEY_RPK, Hex.encodeHexString(publicKey.getEncoded()));
} else if (principal instanceof X500Principal || principal instanceof X509CertPath) {
peer.set(KEY_DN, principal.getName());
}
}
/** copy the attributes **/
Set<Entry<String, String>> attributes = context.entrySet();
if (!attributes.isEmpty()) {
JsonObject attContext = Json.object();
for (Entry<String, String> e : attributes) {
attContext.set(e.getKey(), e.getValue());
}
peer.set(KEY_ATTRIBUTES, attContext);
}
return peer;
}
示例10: serialize
import org.eclipse.leshan.util.Hex; //导入依赖的package包/类
@Override
public JsonElement serialize(SecurityInfo src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject element = new JsonObject();
element.addProperty("endpoint", src.getEndpoint());
if (src.getIdentity() != null) {
JsonObject psk = new JsonObject();
psk.addProperty("identity", src.getIdentity());
psk.addProperty("key", Hex.encodeHexString(src.getPreSharedKey()));
element.add("psk", psk);
}
if (src.getRawPublicKey() != null) {
JsonObject rpk = new JsonObject();
PublicKey rawPublicKey = src.getRawPublicKey();
if (rawPublicKey instanceof ECPublicKey) {
ECPublicKey ecPublicKey = (ECPublicKey) rawPublicKey;
// Get x coordinate
byte[] x = ecPublicKey.getW().getAffineX().toByteArray();
if (x[0] == 0)
x = Arrays.copyOfRange(x, 1, x.length);
rpk.addProperty("x", Hex.encodeHexString(x));
// Get Y coordinate
byte[] y = ecPublicKey.getW().getAffineY().toByteArray();
if (y[0] == 0)
y = Arrays.copyOfRange(y, 1, y.length);
rpk.addProperty("y", Hex.encodeHexString(y));
// Get Curves params
rpk.addProperty("params", ecPublicKey.getParams().toString());
} else {
throw new JsonParseException("Unsupported Public Key Format (only ECPublicKey supported).");
}
element.add("rpk", rpk);
}
return element;
}
示例11: security_info_psk_ser_des_then_equal
import org.eclipse.leshan.util.Hex; //导入依赖的package包/类
@Test
public void security_info_psk_ser_des_then_equal() {
SecurityInfo si = SecurityInfo.newPreSharedKeyInfo("myendPoint", "pskIdentity",
Hex.decodeHex("deadbeef".toCharArray()));
byte[] data = SecurityInfoSerDes.serialize(si);
assertEquals("{\"ep\":\"myendPoint\",\"id\":\"pskIdentity\",\"psk\":\"deadbeef\"}", new String(data));
assertEquals(si, SecurityInfoSerDes.deserialize(data));
}
示例12: createObserveResponse
import org.eclipse.leshan.util.Hex; //导入依赖的package包/类
private ObserveResponse createObserveResponse(Observation observation, LwM2mModel model, Response coapResponse) {
// CHANGED response is supported for backward compatibility with old spec.
if (coapResponse.getCode() != CoAP.ResponseCode.CHANGED
&& coapResponse.getCode() != CoAP.ResponseCode.CONTENT) {
throw new InvalidResponseException("Unexpected response code [%s] for %s", coapResponse.getCode(),
observation);
}
// get content format
ContentFormat contentFormat = null;
if (coapResponse.getOptions().hasContentFormat()) {
contentFormat = ContentFormat.fromCode(coapResponse.getOptions().getContentFormat());
}
// decode response
try {
List<TimestampedLwM2mNode> timestampedNodes = decoder.decodeTimestampedData(coapResponse.getPayload(),
contentFormat, observation.getPath(), model);
// create lwm2m response
if (timestampedNodes.size() == 1 && !timestampedNodes.get(0).isTimestamped()) {
return new ObserveResponse(toLwM2mResponseCode(coapResponse.getCode()),
timestampedNodes.get(0).getNode(), null, observation, null, coapResponse);
} else {
return new ObserveResponse(toLwM2mResponseCode(coapResponse.getCode()), null, timestampedNodes,
observation, null, coapResponse);
}
} catch (CodecException e) {
if (LOG.isDebugEnabled()) {
byte[] payload = coapResponse.getPayload() == null ? new byte[0] : coapResponse.getPayload();
LOG.debug(String.format("Unable to decode notification payload [%s] of observation [%s] ",
Hex.encodeHexString(payload), observation), e);
}
throw new InvalidResponseException(e, "Unable to decode notification payload of observation [%s] ",
observation);
}
}
示例13: decode_device_object
import org.eclipse.leshan.util.Hex; //导入依赖的package包/类
@Test
public void decode_device_object() throws TlvException {
// // the /3// from liwblwm2m
String dataStr = "C800144F70656E204D6F62696C6520416C6C69616E6365C801164C69676874776569676874204D324D20436C69656E74C80209333435303030313233C303312E30860641000141010588070842000ED842011388870841007D42010384C10964C10A0F830B410000C40D5182428FC60E2B30323A3030C10F55";
byte[] bytes = Hex.decodeHex(dataStr.toCharArray());
ByteBuffer b = ByteBuffer.wrap(bytes);
Tlv[] tlv = TlvDecoder.decode(b);
LOG.debug(Arrays.toString(tlv));
ByteBuffer buff = TlvEncoder.encode(tlv);
assertTrue(Arrays.equals(bytes, buff.array()));
}
示例14: decode_object_link
import org.eclipse.leshan.util.Hex; //导入依赖的package包/类
@Test
public void decode_object_link() throws TlvException {
String dataStr = "12345678";
byte[] bytes = Hex.decodeHex(dataStr.toCharArray());
ObjectLink objlnk = TlvDecoder.decodeObjlnk(bytes);
assertEquals(0x1234, objlnk.getObjectId());
assertEquals(0x5678, objlnk.getObjectInstanceId());
dataStr = "ffffffff";
bytes = Hex.decodeHex(dataStr.toCharArray());
objlnk = TlvDecoder.decodeObjlnk(bytes);
assertEquals(0xffff, objlnk.getObjectId());
assertEquals(0xffff, objlnk.getObjectInstanceId());
}
示例15: deserialize
import org.eclipse.leshan.util.Hex; //导入依赖的package包/类
public static EndpointContext deserialize(JsonObject peer) {
String address = peer.get(KEY_ADDRESS).asString();
int port = peer.get(KEY_PORT).asInt();
InetSocketAddress socketAddress = new InetSocketAddress(address, port);
Principal principal = null;
JsonValue value = peer.get(KEY_ID);
if (value != null) {
principal = new PreSharedKeyIdentity(value.asString());
} else if ((value = peer.get(KEY_RPK)) != null) {
try {
byte[] rpk = Hex.decodeHex(value.asString().toCharArray());
X509EncodedKeySpec spec = new X509EncodedKeySpec(rpk);
PublicKey publicKey = KeyFactory.getInstance("EC").generatePublic(spec);
principal = new RawPublicKeyIdentity(publicKey);
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
throw new IllegalStateException("Invalid security info content", e);
}
} else if ((value = peer.get(KEY_DN)) != null) {
principal = new X500Principal(value.asString());
}
EndpointContext endpointContext;
value = peer.get(KEY_ATTRIBUTES);
if (value == null) {
endpointContext = new AddressEndpointContext(socketAddress, principal);
} else {
int index = 0;
String attributes[] = new String[value.asObject().size() * 2];
for (Member member : value.asObject()) {
attributes[index++] = member.getName();
attributes[index++] = member.getValue().asString();
}
endpointContext = new MapBasedEndpointContext(socketAddress, principal, attributes);
}
return endpointContext;
}