下面列出了 org.apache.commons.codec.binary.Hex # encodeHexString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Map<String, String> bin(String hash) throws DecoderException {
Random r = new Random(0);
byte[] h = Hex.decodeHex(hash.substring(2 * checksumOption.getChecksumLength()).toCharArray());
BitSet vector = BitSet.valueOf(h);
int n = vector.length();
Map<String, String> ret = new HashMap<>();
boolean singleHash = hashes.size() == 1;
for (int numHashes : hashes) {
BitSet projection = new BitSet();
for (int i = 0; i < numHashes; ++i) {
int index = r.nextInt(n);
projection.set(i, vector.get(index));
}
String outputHash = numHashes + Hex.encodeHexString(projection.toByteArray());
if (singleHash) {
ret.put(TLSH_BIN_KEY, outputHash);
} else {
ret.put(TLSH_BIN_KEY + "_" + numHashes, outputHash);
}
}
return ret;
}
static String digestToken(String originalToken, String salt) {
try {
ACCESS_TOKEN_LOGGER.debug("Generating secret using algorithm: {} with spec: DEFAULT_ITERATIONS: {}, DESIRED_KEY_LENGTH: {}", KEY_ALGORITHM, DEFAULT_ITERATIONS, DESIRED_KEY_LENGTH);
SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
SecretKey key = factory.generateSecret(new PBEKeySpec(originalToken.toCharArray(), salt.getBytes(), DEFAULT_ITERATIONS, DESIRED_KEY_LENGTH));
return Hex.encodeHexString(key.getEncoded());
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new RuntimeException(e);
}
}
public static String getPublicKeySshFingerprint(RSAPublicKey publicKey) throws NoSuchAlgorithmException, IOException {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] derEncoded = getDerEncoding(publicKey);
byte[] fingerprint = digest.digest(derEncoded);
return Hex.encodeHexString(fingerprint);
}
public String getTokenIdent(String token, byte[] tokenSipHashkey) {
long ident = SipHashInline.hash24_palindromic(tokenSipHashkey, token.getBytes());
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.putLong(ident);
return Hex.encodeHexString(buffer.array());
}
public static String bytesToHex(byte[] bytes) {
if (bytes == null) {
return "(null)";
} else if (bytes.length == 0) {
return "(empty)";
} else {
return Hex.encodeHexString(bytes);
}
}
@Override
public void generateAddress(EmptyMessage request,
StreamObserver<AddressPrKeyPairMessage> responseObserver) {
ECKey ecKey = new ECKey(Utils.getRandom());
byte[] priKey = ecKey.getPrivKeyBytes();
byte[] address = ecKey.getAddress();
String addressStr = Wallet.encode58Check(address);
String priKeyStr = Hex.encodeHexString(priKey);
AddressPrKeyPairMessage.Builder builder = AddressPrKeyPairMessage.newBuilder();
builder.setAddress(addressStr);
builder.setPrivateKey(priKeyStr);
responseObserver.onNext(builder.build());
responseObserver.onCompleted();
}
/**
* Calculates the MD5 checksum for {@link #CONTENTS}
* @return the checksum
*/
private String calculateChecksum() {
MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
md5.update(CONTENTS.getBytes(StandardCharsets.UTF_8));
return Hex.encodeHexString(md5.digest());
}
@Override
public String toString() {
return "BlobA6{"
+ "type=0x" + Integer.toHexString(type())
+ ",length=0x" + Integer.toHexString(length())
+ ", x=" + x
+ ", tag=0x" + Hex.encodeHexString(tag)
+ ", m2=0x" + Hex.encodeHexString(m2())
+ ", iv=0x" + Hex.encodeHexString(iv())
+ ", data=0x" + Hex.encodeHexString(data())
+ '}';
}
/**
* AES-PKCS7算法加密数据
* 兼容IOS中的SecKeyWrapper加解密(SecKeyWrapper采用的是PKCS7Padding填充方式)
* @param data 待加密的明文字符串
* @param key AES密钥字符串
* @return AES-PKCS7加密后的16进制表示的密文字符串,加密过程中遇到异常则抛出RuntimeException
*/
public static String buildAESPKCS7Encrypt(String data, String key){
Security.addProvider(new BouncyCastleProvider());
try{
SecretKey secretKey = new SecretKeySpec(Hex.decodeHex(key.toCharArray()), ALGORITHM_AES_PKCS7);
Cipher cipher = Cipher.getInstance(ALGORITHM_CIPHER_AES_PKCS7);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, initIV());
return Hex.encodeHexString(cipher.doFinal(data.getBytes(SeedConstants.DEFAULT_CHARSET)));
}catch(Exception e){
throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e);
}
}
private static <T> Java getByteCodeVersion(final Class<T> clazz) throws IOException {
final InputStream stream =
clazz.getClassLoader().getResourceAsStream(clazz.getName().replace(".", "/") + ".class");
final byte[] classBytes = IOUtils.toByteArray(stream);
final String versionInHexString =
Hex.encodeHexString(new byte[] {classBytes[6], classBytes[7]});
return versionMapping.get(versionInHexString);
}
public static String checksum(String data) throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] b = data == null ? new byte[0] : data.getBytes(UTF_8);
md5.update(b, 0, b.length);
return Hex.encodeHexString(md5.digest());
}
@Override
public Checksum compute(final InputStream in, final TransferStatus status) throws ChecksumException {
return new Checksum(HashAlgorithm.sha512, Hex.encodeHexString(this.digest("SHA-512",
this.normalize(in, status))));
}
public static String md5Hex(String utf8Content) {
return Hex.encodeHexString(computeMD5Hash(utf8Content.getBytes(StringUtils.UTF8)));
}
private String getSha() {
return Hex.encodeHexString(HashHelper.sha256().digest(content));
}
public static String getSigningSignature(byte[] key, String data) throws Exception {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key, "HmacSHA256");
sha256_HMAC.init(secret_key);
return Hex.encodeHexString(sha256_HMAC.doFinal(data.getBytes("UTF-8")));
}
private String base64ToHex(String base64) {
return (base64 != null) ? Hex.encodeHexString(Base64.decodeBase64(base64)) : null;
}
/**
* Hex编码.
*/
public static String encodeHex(byte[] input) {
return Hex.encodeHexString(input);
}
/**
* 计算区块的hash值
*
* @param block
* 区块
* @return
*/
public static String calculateHash(Block block) {
String record = (block.getIndex()) + block.getTimestamp() + (block.getVac()) + block.getPrevHash();
MessageDigest digest = DigestUtils.getSha256Digest();
byte[] hash = digest.digest(StringUtils.getBytesUtf8(record));
return Hex.encodeHexString(hash);
}
/**
* Create hex string from ascii
*
* @param string
* @return converted hex string
*/
public static String encodeHex(String string){
return Hex.encodeHexString(String.valueOf(string).getBytes());
}
/**
* Converts a binary value to a encoded string
*
* @param data Binary value to encode as an encoded string
* @return Encoded string from the binary value specified
*/
private String toString(final byte[] data) {
return Hex.encodeHexString(data);
}