下面列出了 org.apache.commons.codec.binary.Hex # encodeHex ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private String encodeInternal(String input)
{
if (!getEncodeHashAsBase64())
{
return new String(Hex.encodeHex(md4(input)));
}
byte[] encoded = Base64.encodeBase64(md4(input));
try
{
return new String(encoded, "UTF8");
}
catch (UnsupportedEncodingException e)
{
throw new RuntimeException("UTF8 not supported!", e);
}
}
public String convertToDatabaseColumn(String phrase) throws SQLException {
try {
Cipher encryptCipher = Cipher.getInstance("AES");
encryptCipher.init(Cipher.ENCRYPT_MODE,
generateMySQLAESKey(this.key, "UTF-8"));
return new String(Hex.encodeHex(encryptCipher.doFinal(phrase.getBytes("UTF-8"))));
} catch (Exception e) {
throw new SQLException(e);
}
}
public static String md5(String plaintext) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
return new String(Hex.encodeHex(md.digest(plaintext.getBytes())));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
@Override
public String getHash() {
byte[] toHash = Helpers.hexStringFromUuid(database.getRootGroup().getUuid()).getBytes();
SHA1Digest digest = new SHA1Digest();
byte[] digestBytes = new byte[digest.getDigestSize()];
digest.update(toHash, 0, toHash.length);
digest.doFinal(digestBytes, 0);
String result = new String(Hex.encodeHex(digestBytes));
return result.toLowerCase();
}
/**
* Sign the given message using the given secret.
* @param message message to sign
* @param secret secret key
* @return a signed message
*/
public static String sign(String message, String secret) {
try {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
sha256_HMAC.init(secretKeySpec);
return new String(Hex.encodeHex(sha256_HMAC.doFinal(message.getBytes())));
} catch (Exception e) {
throw new RuntimeException("Unable to sign message.", e);
}
}
/********************************************
* Convert BouncyCastle PGPKey to SecurityKey entity
*/
public static PublicKeyStore convert( PGPPublicKey pgpKey ) throws IOException
{
String fingerprint = new String( Hex.encodeHex( pgpKey.getFingerprint(), false ) );
PublicKeyStore pk = new PublicKeyStoreEntity();
pk.setFingerprint( fingerprint );
pk.setKeyId( PGPKeyUtil.getKeyId( fingerprint ) );
pk.setShortKeyId( PGPKeyUtil.getShortKeyId( fingerprint ) );
pk.setKeyData( pgpKey.getPublicKeyPacket().getEncoded() );
return pk;
}
/**
* Stores the hash of a written content file in the transaction for potential clients to pick up.
*
* @param writer
* the writer used to write the content file
*/
protected void txnStoreHash(final ArchvieFileContentWriterImpl writer)
{
final Map<String, String> map = TransactionalResourceHelper.getMap(TXN_CONTENT_URL_HASHES);
final byte[] digest = writer.getDigest();
final char[] digestCh = Hex.encodeHex(digest, false);
final String digestStr = new String(digestCh);
final String digestVal = this.digestAlgorithm.toLowerCase(Locale.ENGLISH) + ':' + digestStr;
map.put(writer.getContentUrl(), digestVal);
}
public static String sha1(String plaintext) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
return new String(Hex.encodeHex(md.digest(plaintext.getBytes())));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
/**
* 文件转MD5Hash
*
* @param fis
* @return
*/
public static String md5HashCode(InputStream fis) {
try {
MessageDigest MD5 = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[8192];
int length;
while ((length = fis.read(buffer)) != -1) {
MD5.update(buffer, 0, length);
}
return new String(Hex.encodeHex(MD5.digest()));
} catch (Exception e) {
logger.error(e.getMessage());
return null;
}
}
public String encodePassword(String rawPass, String salt) {
String saltedPass = mergePasswordAndSalt(rawPass, salt, false);
MessageDigest messageDigest = getMessageDigest();
byte[] digest;
try {
digest = messageDigest.digest(saltedPass.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 not supported!");
}
return new String(Hex.encodeHex(digest));
}
public static String generateSignature(String appCertificate, short service, String appID, int unixTs, int salt, String channelName, long uid, int expiredTs, TreeMap<Short, String> extra) throws Exception {
// decode hex to avoid case problem
Hex hex = new Hex();
byte[] rawAppID = hex.decode(appID.getBytes());
byte[] rawAppCertificate = hex.decode(appCertificate.getBytes());
Message m = new Message(service, rawAppID, unixTs, salt, channelName, (int)(uid & 0xFFFFFFFFL), expiredTs, extra);
byte[] toSign = pack(m);
return new String(Hex.encodeHex(DynamicKeyUtil.encodeHMAC(rawAppCertificate, toSign), false));
}
public String getGlobalUserId() {
if (StringUtils.isEmpty(this.globalUserId)) {
String body = this.userIdPubk;
String SHA1UserPubKey = new String(Hex.encodeHex(DigestUtils.sha1(body)));
CRC32 c32 = new CRC32();
c32.update(body.getBytes(), 0, body.getBytes().length);
String CRC32UserPubKey = String.valueOf(c32.getValue());
return SHA1UserPubKey + "-" + CRC32UserPubKey;
}
return this.globalUserId;
}
public String getGlobalUserId() {
if (StringUtils.isEmpty(this.globalUserId)) {
String body = this.userIdPubk;
String SHA1UserPubKey = new String(Hex.encodeHex(DigestUtils.sha1(body)));
CRC32 c32 = new CRC32();
c32.update(body.getBytes(), 0, body.getBytes().length);
String CRC32UserPubKey = String.valueOf(c32.getValue());
return SHA1UserPubKey + "-" + CRC32UserPubKey;
}
return this.globalUserId;
}
/**
* Hex编码.
*/
public static String encodeHex(byte[] input) {
return new String(Hex.encodeHex(input));
}
@Override
public String checksum() {
String str = preChecksum();
return new String(Hex.encodeHex(DigestUtils.sha1(str)));
}
/**
* Hex编码.
*/
public static String encodeHex(byte[] input) {
return new String(Hex.encodeHex(input));
}
public static String hash(String provider, String id) {
return new String(Hex.encodeHex(DigestUtils.sha256(provider + ":" + id)));
}
public static String toHex(byte[] bytes) {
return new String(Hex.encodeHex(bytes));
}
/**
* The encryption implement for AES-128 algorithm for BCE password encryption.
* Only the first 16 bytes of privateKey will be used to encrypt the content.
* <p>
* See more detail on
* <a href = "https://bce.baidu.com/doc/BCC/API.html#.7A.E6.31.D8.94.C1.A1.C2.1A.8D.92.ED.7F.60.7D.AF">
* BCE API doc</a>
*
* @param content The content String to encrypt.
* @param privateKey The security key to encrypt.
* Only the first 16 bytes of privateKey will be used to encrypt the content.
* @return The encrypted string of the original content with AES-128 algorithm.
* @throws GeneralSecurityException
*/
private String aes128WithFirst16Char(String content, String privateKey) throws GeneralSecurityException {
byte[] crypted = null;
SecretKeySpec skey = new SecretKeySpec(privateKey.substring(0, 16).getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skey);
crypted = cipher.doFinal(content.getBytes());
return new String(Hex.encodeHex(crypted));
}
/**
* Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
* <p>
* This method is just there to avoid consumers using commons-codec directly.
*
* @param bytes the bytes
* @return the hexadecimal characters.
*/
@Override
public char[] hex(byte[] bytes) {
return Hex.encodeHex(bytes);
}