List of usage examples for org.apache.commons.codec.binary Hex encodeHex
public static char[] encodeHex(byte[] data)
From source file:cdr.forms.DepositFile.java
public String getHexDigest(String algorithm) throws NoSuchAlgorithmException, IOException { return new String(Hex.encodeHex(this.getDigest(algorithm))); }
From source file:com.haulmont.timesheets.EncryptDecrypt.java
private static String encodeString(byte[] raw) { if (raw == null) return null; return new String(Hex.encodeHex(raw)); }
From source file:com.amazonaws.services.dynamodbv2.streams.connectors.DynamoDBConnectorUtilities.java
/** * Get the taskname from command line arguments if it exists, if not, autogenerate one to be used by KCL in the * checkpoint table and to publish CloudWatch metrics * * @param sourceRegion/* www . j ava2 s. c o m*/ * region of the source table * @param destinationRegion * region of the destination table * @param params * command line arguments, use to check if there exists an user-specified task name * @return the generated task name * @throws UnsupportedEncodingException */ public static String getTaskName(Region sourceRegion, Region destinationRegion, CommandLineArgs params) throws UnsupportedEncodingException { String taskName; if (params.getTaskName() != null) { taskName = DynamoDBConnectorConstants.SERVICE_PREFIX + params.getTaskName(); if (taskName.length() > DynamoDBConnectorConstants.DYNAMODB_TABLENAME_LIMIT) { throw new ParameterException("Provided taskname is too long!"); } } else { taskName = sourceRegion + params.getSourceTable() + destinationRegion + params.getDestinationTable(); // hash stack name using MD5 if (DynamoDBConnectorConstants.MD5_DIGEST == null) { // see if we can generate a taskname without hashing if (taskName.length() > DynamoDBConnectorConstants.DYNAMODB_TABLENAME_LIMIT) { // must hash the taskname throw new IllegalArgumentException( "Generated taskname is too long and cannot be hashed due to improperly initialized MD5 digest object!"); } } else { taskName = DynamoDBConnectorConstants.SERVICE_PREFIX + new String(Hex.encodeHex(DynamoDBConnectorConstants.MD5_DIGEST .digest(taskName.getBytes(DynamoDBConnectorConstants.BYTE_ENCODING)))); } } return taskName; }
From source file:com.antelink.sourcesquare.client.scan.FileAnalyzer.java
/** * Computes the hash of a given file./*from w w w . ja v a 2 s. c om*/ * * @param hashType * Hash that you want to compute. Usually SHA-1 or MD5 (or any * other kind supported by java.security.MessageDigest). * @param fileToAnalyze * Descriptor of the file to analyze. Must be a file otherwise * you will get an exception. * @return the String hexadecimal representation of the file hash. * @throws NoSuchAlgorithmException * if hashType is not a supported hash algorithm * @throws IOException * in case of an I/O error while reading file */ public static String calculateHash(String hashType, File fileToAnalyze) throws NoSuchAlgorithmException, IOException { FileInputStream fis = null; BufferedInputStream inputStream = null; try { logger.debug("Calculating sha1 for file " + fileToAnalyze.getName()); fis = new FileInputStream(fileToAnalyze); inputStream = new BufferedInputStream(fis); MessageDigest digest = MessageDigest.getInstance(hashType); byte[] buffer = new byte[1024 * 1024]; int length = -1; while ((length = inputStream.read(buffer)) != -1) { digest.update(buffer, 0, length); } String hash = new String(Hex.encodeHex(digest.digest())); logger.debug("Sha1 calculated for file " + fileToAnalyze.getName()); return hash; } finally { IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(fis); } }
From source file:com.dtolabs.rundeck.core.execution.commands.ScriptURLCommandInterpreter.java
private static String hashURL(final String url) { try {// w w w .ja va 2 s.com MessageDigest digest = MessageDigest.getInstance("SHA-1"); digest.reset(); digest.update(url.getBytes(Charset.forName("UTF-8"))); return new String(Hex.encodeHex(digest.digest())); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return Integer.toString(url.hashCode()); }
From source file:com.mastercard.mobile_api.utils.Tlv.java
/** * Creates a Tlv byte array from a tag and a value. Length is calculated. * Data Input is in Hex String/*from ww w . jav a 2 s . c o m*/ * * @param tag the Tlv tag (HEX String) * @param value the Tlv value (HEX String) * @return the Tlv byte array (HEX String) */ public static String create(String tag, String value) { byte[] bValue = new byte[0]; try { bValue = Hex.decodeHex(value.toCharArray()); } catch (DecoderException e) { e.printStackTrace(); } byte[] bLength = lengthBytes(bValue); String length = new String(Hex.encodeHex(bLength)); return (tag + length + value).toUpperCase(); }
From source file:net.padlocksoftware.padlock.tools.LicenseValidator.java
public String validate() { Date currentDate = new Date(); Validator v = new Validator(license, new String(Hex.encodeHex(pair.getPublic().getEncoded()))); v.setIgnoreFloatTime(true);//w w w . j a va 2s. c om LicenseState state; try { state = v.validate(); } catch (ValidatorException e) { state = e.getLicenseState(); } StringBuilder builder = new StringBuilder(); // Show test status builder.append("\nValidation Test Results:\n"); builder.append("========================\n\n"); for (TestResult result : state.getTests()) { builder.append( "\t" + result.getTest().getName() + "\t\t\t" + (result.passed() ? "Passed" : "Failed") + "\n"); } builder.append("\nLicense state: " + (state.isValid() ? "Valid" : "Invalid" + "\n")); // // Cycle through any dates // Date d = license.getCreationDate(); builder.append("\nCreation date: \t\t" + d + "\n"); d = license.getStartDate(); builder.append("Start date: \t\t" + d + "\n"); d = license.getExpirationDate(); builder.append("Expiration date: \t" + d + "\n"); Long floatPeroid = license.getFloatingExpirationPeriod(); if (floatPeroid != null) { long seconds = floatPeroid / 1000L; builder.append("\nExpire after first run: " + seconds + " seconds\n"); } if (floatPeroid != null || license.getExpirationDate() != null) { long remaining = v.getTimeRemaining(currentDate) / 1000L; builder.append("\nTime remaining: " + remaining + " seconds\n"); } // // License properties // builder.append("\nLicense Properties\n"); Properties p = license.getProperties(); if (p.size() == 0) { builder.append("None\n"); } for (final Enumeration propNames = p.propertyNames(); propNames.hasMoreElements();) { final String key = (String) propNames.nextElement(); builder.append("Property: " + key + " = " + p.getProperty(key) + "\n"); } // // Hardware locking // for (String address : license.getHardwareAddresses()) { builder.append("\nHardware lock: " + address + "\n"); } builder.append("\n"); return builder.toString(); }
From source file:com.ning.arecibo.util.timeline.times.TimesAndSamplesCoder.java
public static byte[] combineTimesAndSamples(final byte[] times, final byte[] samples) { final int totalSamplesSize = 4 + times.length + samples.length; final ByteArrayOutputStream baStream = new ByteArrayOutputStream(totalSamplesSize); final DataOutputStream outputStream = new DataOutputStream(baStream); try {/*ww w.j a v a 2s. c o m*/ outputStream.writeInt(times.length); outputStream.write(times); outputStream.write(samples); outputStream.flush(); return baStream.toByteArray(); } catch (IOException e) { throw new IllegalStateException(String.format( "Exception reading timeByteCount in TimelineChunkMapper.map() for times %s, samples %s", Hex.encodeHex(times), Hex.encodeHex(samples)), e); } }
From source file:com.zimbra.cs.service.mail.SendVerificationCode.java
static String generateVerificationCode() { ZimbraLog.misc.debug("Generating verification code"); SecureRandom random = new SecureRandom(); byte bytes[] = new byte[3]; random.nextBytes(bytes);// w w w. j a va 2s .com return new String(Hex.encodeHex(bytes)); }
From source file:com.dynamobi.ws.util.LucidDBEncoder.java
public String encodePassword(String rawPass, Object salt) { // support a case: password is null. if (rawPass.isEmpty()) { return ""; }// ww w . ja v a 2 s.co m String saltedPass = mergePasswordAndSalt(rawPass, salt, false); MessageDigest messageDigest = getMessageDigest(); byte[] digest; try { digest = messageDigest.digest(saltedPass.getBytes("UTF-16LE")); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("UTF-16LE not supported!"); } // System.out.println("ZZZZ Base64 [" // + new String(Base64.encodeBase64(digest)) + "] Hex = [" // + new String(Hex.encodeHex(digest)) + "]"); if (getEncodeHashAsBase64()) { return new String(Base64.encodeBase64(digest)); } else { return new String(Hex.encodeHex(digest)); } }