下面列出了怎么用javax.xml.bind.annotation.adapters.HexBinaryAdapter的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 生成 HMAC_SHA256
*
* @param content
* @param api_key
* @return
* @throws Exception
*/
public static String HMAC_SHA256(String content, String api_key) {
try {
KeyGenerator generator = KeyGenerator.getInstance("HmacSHA256");
SecretKey secretKey = generator.generateKey();
byte[] key = secretKey.getEncoded();
SecretKey secretKeySpec = new SecretKeySpec(api_key.getBytes(), "HmacSHA256");
Mac mac = Mac.getInstance(secretKeySpec.getAlgorithm());
mac.init(secretKeySpec);
byte[] digest = mac.doFinal(content.getBytes());
return new HexBinaryAdapter().marshal(digest).toLowerCase();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String createSha1(File file) throws TechnicalException {
try (InputStream fis = new FileInputStream(file);) {
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
int n = 0;
byte[] buffer = new byte[8192];
while (n != -1) {
n = fis.read(buffer);
if (n > 0) {
sha1.update(buffer, 0, n);
}
}
return new HexBinaryAdapter().marshal(sha1.digest());
} catch (NoSuchAlgorithmException | IOException e) {
throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_CHECKSUM_IO_EXCEPTION), e);
}
}
public static String encodeMd5(byte[] data) {
// 初始化MessageDigest
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 执行摘要信息
byte[] digest = md.digest(data);
// 将摘要信息转换为32位的十六进制字符串
return new String(new HexBinaryAdapter().marshal(digest));
}
/**
* 生成md5摘要
*
* @param content
* @return
*/
public static String MD5(String content) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(content.getBytes(StandardCharsets.UTF_8));
byte[] hashCode = messageDigest.digest();
return new HexBinaryAdapter().marshal(hashCode).toLowerCase();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Gets the unique ID.
*
* @param idstring
* the idstring
* @return the unique ID
*/
public static String getUniqueID(String idstring) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
return (new HexBinaryAdapter()).marshal(md5.digest(idstring.getBytes()));
} catch (NoSuchAlgorithmException e) {
LOGGER.error("Error in getUniqueID",e);
}
return "";
}
/**
* Gets the unique ID.
*
* @param idstring
* the idstring
* @return the unique ID
*/
public static String getUniqueID(String idstring) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
return (new HexBinaryAdapter()).marshal(md5.digest(idstring.getBytes()));
} catch (NoSuchAlgorithmException e) {
LOGGER.error("Error in getUniqueID",e);
}
return "";
}
/**
* Gets the unique ID.
*
* @param idstring
* the idstring
* @return the unique ID
*/
public static String getUniqueID(String idstring) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
return (new HexBinaryAdapter()).marshal(md5.digest(idstring.getBytes()));
} catch (NoSuchAlgorithmException e) {
LOGGER.error("Error in getUniqueID",e);
}
return "";
}
public String encrpPass(String password){
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return (new HexBinaryAdapter()).marshal(md5.digest(password.getBytes()));
}
public String encrpPass(String password){
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return (new HexBinaryAdapter()).marshal(md5.digest(password.getBytes()));
}
public String encrpPass(String password){
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return (new HexBinaryAdapter()).marshal(md5.digest(password.getBytes()));
}
/**
* Converts an arbitrary message into an MD5 hash and returns it as a UTF-8 encoded string
*
* @param message is the message to convert
* @return a UTF-8 encoded string representation of the MD5 hash
*/
String getMd5Hash(String message) {
String result;
try {
MessageDigest md5 = MessageDigest.getInstance(MD5);
result = (new HexBinaryAdapter()).marshal(md5.digest(message.getBytes())).toLowerCase();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Error converting message to MD5 hash", e);
}
return result;
}
/**
* Creates a {@link BNode} or Skolem {@link IRI} object for the specified identifier.
*/
protected Resource createNode(String nodeID) throws RDFParseException {
// If we are preserving blank node ids then we do not prefix them to
// make them globally unique
if (preserveBNodeIDs()) {
return valueFactory.createBNode(nodeID);
} else {
// Prefix the node ID with a unique UUID prefix to reduce
// cross-document clashes
// This is consistent as long as nextBNodePrefix is not modified
// between parser runs
String toAppend = nodeID;
if (nodeID.length() > 32) {
// we only hash the node ID if it is longer than the hash string
// itself would be.
byte[] chars = nodeID.getBytes(StandardCharsets.UTF_8);
// we use an MD5 hash rather than the node ID itself to get a
// fixed-length generated id, rather than
// an ever-growing one (see SES-2171)
toAppend = (new HexBinaryAdapter()).marshal(md5.digest(chars));
}
String origin = parserConfig.get(BasicParserSettings.SKOLEMIZE_ORIGIN);
if (origin == null || origin.length() == 0) {
return valueFactory.createBNode("genid-" + nextBNodePrefix + toAppend);
} else {
String path = "/.well-known/genid/" + nextBNodePrefix + toAppend;
String iri = ParsedIRI.create(origin).resolve(path);
return valueFactory.createIRI(iri);
}
}
}
/**
* Creates a {@link BNode} object for the specified identifier.
*/
@Deprecated
protected BNode createBNode(String nodeID) throws RDFParseException {
// If we are preserving blank node ids then we do not prefix them to
// make them globally unique
if (preserveBNodeIDs()) {
return valueFactory.createBNode(nodeID);
} else {
// Prefix the node ID with a unique UUID prefix to reduce
// cross-document clashes
// This is consistent as long as nextBNodePrefix is not modified
// between parser runs
String toAppend = nodeID;
if (nodeID.length() > 32) {
// we only hash the node ID if it is longer than the hash string
// itself would be.
byte[] chars = nodeID.getBytes(StandardCharsets.UTF_8);
// we use an MD5 hash rather than the node ID itself to get a
// fixed-length generated id, rather than
// an ever-growing one (see SES-2171)
toAppend = (new HexBinaryAdapter()).marshal(md5.digest(chars));
}
return valueFactory.createBNode("genid-" + nextBNodePrefix + toAppend);
}
}
public static String calculateMD5Sum(String inStr) {
MessageDigest hasher = null;
try{
hasher = java.security.MessageDigest.getInstance("MD5");
}
catch(NoSuchAlgorithmException e){
LOG.error("Wrong hashing algorithm name provided while getting instance of MessageDigest: " + e.getLocalizedMessage());
}
return (new HexBinaryAdapter()).marshal(hasher.digest(inStr.getBytes()));
}
private ImmutableSortedSet<String> getExistingDsData(DomainBase domain) {
ImmutableSortedSet.Builder<String> dsDataJsons = ImmutableSortedSet.naturalOrder();
HexBinaryAdapter hexBinaryAdapter = new HexBinaryAdapter();
for (DelegationSignerData dsData : domain.getDsData()) {
dsDataJsons.add(JSONValue.toJSONString(ImmutableMap.of(
"keyTag", dsData.getKeyTag(),
"algorithm", dsData.getAlgorithm(),
"digestType", dsData.getDigestType(),
"digest", hexBinaryAdapter.marshal(dsData.getDigest()))));
}
return dsDataJsons.build();
}
private void persistDomainWithHosts(HostResource... hosts) {
ImmutableSet.Builder<VKey<HostResource>> hostRefs = new ImmutableSet.Builder<>();
for (HostResource host : hosts) {
hostRefs.add(host.createVKey());
}
persistResource(newDomainBase("evil.tld").asBuilder()
.setNameservers(hostRefs.build())
.setDsData(ImmutableSet.of(
DelegationSignerData.create(1, 2, 3, new HexBinaryAdapter().unmarshal("dead")),
DelegationSignerData.create(4, 5, 6, new HexBinaryAdapter().unmarshal("beef"))))
.build());
}
private static void processXmlSchemaSimpleType(XmlSchemaSimpleType xmlSchema, JavaMethod method,
MessagePartInfo part) {
if (xmlSchema.getContent() instanceof XmlSchemaSimpleTypeList
&& (!part.isElement() || !method.isWrapperStyle())) {
method.annotate(new XmlListAnotator(method.getInterface()));
}
if (Constants.XSD_HEXBIN.equals(xmlSchema.getQName())
&& (!part.isElement() || !method.isWrapperStyle())) {
method.annotate(new XmlJavaTypeAdapterAnnotator(method.getInterface(), HexBinaryAdapter.class));
}
}
private static void processXmlSchemaSimpleType(XmlSchemaSimpleType xmlSchema, JavaMethod jm,
JavaParameter parameter, MessagePartInfo part) {
if (xmlSchema.getContent() instanceof XmlSchemaSimpleTypeList
&& (!part.isElement() || !jm.isWrapperStyle())) {
parameter.annotate(new XmlListAnotator(jm.getInterface()));
}
if (Constants.XSD_HEXBIN.equals(xmlSchema.getQName())
&& (!part.isElement() || !jm.isWrapperStyle())) {
parameter.annotate(new XmlJavaTypeAdapterAnnotator(jm.getInterface(), HexBinaryAdapter.class));
}
}
public static String encodeSHA(byte[] data) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA");
byte[] digest = md.digest(data);
return new HexBinaryAdapter().marshal(digest);
}
public static String encodeSHA256(byte[] data) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] digest = md.digest(data);
return new HexBinaryAdapter().marshal(digest);
}
public static String encodeSHA384(byte[] data) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-384");
byte[] digest = md.digest(data);
return new HexBinaryAdapter().marshal(digest);
}
public static String encodeSHA512(byte[] data) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-512");
byte[] digest = md.digest(data);
return new HexBinaryAdapter().marshal(digest);
}
public String getMessageDigestAsHexadecimalString (String algorithm)
{
return (new HexBinaryAdapter()).marshal (
getMessageDigest (algorithm).digest ());
}
public String getMessageDigestAsHexadecimalString (String algorithm)
{
return (new HexBinaryAdapter()).marshal (
getMessageDigest (algorithm).digest ());
}
@Test
public void testConfigureSimpleNoMatchingBean() {
SimpleBean sb = new SimpleBean("unknown");
BusApplicationContext ac =
new BusApplicationContext("/org/apache/cxf/configuration/spring/test-beans.xml",
false);
ConfigurerImpl configurer = new ConfigurerImpl(ac);
configurer.configureBean(sb);
assertEquals("Unexpected value for attribute stringAttr",
"hello", sb.getStringAttr());
assertTrue("Unexpected value for attribute booleanAttr",
sb.getBooleanAttr());
assertEquals("Unexpected value for attribute integerAttr",
BigInteger.ONE, sb.getIntegerAttr());
assertEquals("Unexpected value for attribute intAttr",
Integer.valueOf(2), sb.getIntAttr());
assertEquals("Unexpected value for attribute longAttr",
Long.valueOf(3L), sb.getLongAttr());
assertEquals("Unexpected value for attribute shortAttr",
Short.valueOf((short)4), sb.getShortAttr());
assertEquals("Unexpected value for attribute decimalAttr",
new BigDecimal("5"), sb.getDecimalAttr());
assertEquals("Unexpected value for attribute floatAttr",
Float.valueOf(6F), sb.getFloatAttr());
assertEquals("Unexpected value for attribute doubleAttr",
Double.valueOf(7.0D), sb.getDoubleAttr());
assertEquals("Unexpected value for attribute byteAttr",
Byte.valueOf((byte)8), sb.getByteAttr());
QName qn = sb.getQnameAttr();
assertEquals("Unexpected value for attribute qnameAttrNoDefault",
"schema", qn.getLocalPart());
assertEquals("Unexpected value for attribute qnameAttrNoDefault",
"http://www.w3.org/2001/XMLSchema", qn.getNamespaceURI());
byte[] expected = DatatypeConverter.parseBase64Binary("abcd");
byte[] val = sb.getBase64BinaryAttr();
assertEquals("Unexpected value for attribute base64BinaryAttrNoDefault", expected.length, val.length);
for (int i = 0; i < expected.length; i++) {
assertEquals("Unexpected value for attribute base64BinaryAttrNoDefault", expected[i], val[i]);
}
expected = new HexBinaryAdapter().unmarshal("aaaa");
val = sb.getHexBinaryAttr();
assertEquals("Unexpected value for attribute hexBinaryAttrNoDefault", expected.length, val.length);
for (int i = 0; i < expected.length; i++) {
assertEquals("Unexpected value for attribute hexBinaryAttrNoDefault", expected[i], val[i]);
}
assertEquals("Unexpected value for attribute unsignedIntAttrNoDefault",
Long.valueOf(9L), sb.getUnsignedIntAttr());
assertEquals("Unexpected value for attribute unsignedShortAttrNoDefault",
Integer.valueOf(10), sb.getUnsignedShortAttr());
assertEquals("Unexpected value for attribute unsignedByteAttrNoDefault",
Short.valueOf((short)11), sb.getUnsignedByteAttr());
}