/** * Returns base64 representation of specified byte array. */ public static String encode(byte[] data) { return encode(data, 0, data.length); }
private synchronized String generateAgentToken() { if (random == null) { try { random = SecureRandom.getInstance("SHA1PRNG"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Could not load SecureRandom algorithm", e); } } byte[] tokenBytes = new byte[50]; random.nextBytes(tokenBytes); return Base64.encode(tokenBytes); }
/** * @see org.apache.tools.ant.Task#execute() */ public void execute() throws BuildException { validateAttributes(); MessageDigest message_digest; try { message_digest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new BuildException(e); // should never occur; this would be bad - JRE has this builtin } byte[] md5_bytes = message_digest.digest(value.getBytes()); String md5; if (base64) { md5 = Base64.encode(md5_bytes); } else { // put the md5 bytes in string form StringBuilder md5_string = new StringBuilder(md5_bytes.length * 2); for (byte md5_byte : md5_bytes) { int hi = (md5_byte >> 4) & 0xf; int lo = md5_byte & 0xf; md5_string.append(Character.forDigit(hi, 16)); md5_string.append(Character.forDigit(lo, 16)); } md5 = md5_string.toString(); } Project this_project = getProject(); this_project.setNewProperty(property, md5); return; }