List of usage examples for java.math BigInteger toString
public String toString(int radix)
From source file:com.raphfrk.craftproxyclient.net.protocol.p17xlogin.P17xLoginProtocol.java
private static String SHA1Hash(Object[] input) { try {//from ww w . ja va2 s . c om MessageDigest md = MessageDigest.getInstance("SHA-1"); md.reset(); for (Object o : input) { if (o instanceof String) { md.update(((String) o).getBytes("ISO_8859_1")); } else if (o instanceof byte[]) { md.update((byte[]) o); } else { return null; } } byte[] digest = md.digest(); BigInteger bigInt = new BigInteger(digest); return bigInt.toString(16); } catch (Exception ioe) { return null; } }
From source file:MainFrame.HttpCommunicator.java
public static String md5Custom(String st) { MessageDigest messageDigest = null; byte[] digest = new byte[0]; try {/*from www .j a v a 2 s . c o m*/ messageDigest = MessageDigest.getInstance("MD5"); messageDigest.reset(); messageDigest.update(st.getBytes()); digest = messageDigest.digest(); } catch (NoSuchAlgorithmException e) { System.err.printf("MD-5 error"); } BigInteger bigInt = new BigInteger(1, digest); String md5Hex = bigInt.toString(16); while (md5Hex.length() < 32) { md5Hex = "0" + md5Hex; } return md5Hex; }
From source file:org.jspringbot.keyword.expression.ELUtils.java
public static String md5(String str) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("MD5"); byte[] data = str.getBytes(); digest.update(data, 0, data.length); BigInteger i = new BigInteger(1, digest.digest()); return i.toString(16); }
From source file:org.apache.accumulo.master.tableOps.Utils.java
static String getNextTableId(String tableName, Instance instance) throws ThriftTableOperationException { String tableId = null;/*from w ww.ja v a 2 s. com*/ try { IZooReaderWriter zoo = ZooReaderWriter.getRetryingInstance(); final String ntp = ZooUtil.getRoot(instance) + Constants.ZTABLES; byte[] nid = zoo.mutate(ntp, ZERO_BYTE, ZooUtil.PUBLIC, new Mutator() { @Override public byte[] mutate(byte[] currentValue) throws Exception { BigInteger nextId = new BigInteger(new String(currentValue, Constants.UTF8), Character.MAX_RADIX); nextId = nextId.add(BigInteger.ONE); return nextId.toString(Character.MAX_RADIX).getBytes(Constants.UTF8); } }); return new String(nid, Constants.UTF8); } catch (Exception e1) { Logger.getLogger(CreateTable.class).error("Failed to assign tableId to " + tableName, e1); throw new ThriftTableOperationException(tableId, tableName, TableOperation.CREATE, TableOperationExceptionType.OTHER, e1.getMessage()); } }
From source file:watne.seis720.project.AES_Utilities.java
/** * Get a String representing the given array of bytes as a hexadecimal * number. Code adapted from <a/*from w w w .ja va 2s. com*/ * href="http://www.exampledepot.com/egs/java.math/Bytes2Str.html">Parsing * and Formatting a Byte Array into Binary, Octal, and Hexadecimal (Java * Developers Almanac Example)</a> * @param bytes the sequence of bytes for which the hexadecimal String * representation will be given. * @return a String representing the given array of bytes as a hexadecimal * number. */ public static String getHexString(byte[] bytes) { String hexString = null; BigInteger bigInt = new BigInteger(bytes); hexString = bigInt.toString(16); // Get base 16 (hexadecimal) // representation. if (hexString.length() % 2 != 0) { // Make sure an even number of characters: start with a zero. hexString = "0" + hexString; } return hexString; }
From source file:org.wso2.carbon.apimgt.gateway.handlers.analytics.APIMgtGoogleAnalyticsTrackingHandler.java
/** * /*from w w w. j a v a2s .c om*/ * Generate a visitor id for this hit. If there is a visitor id in the * messageContext, use that. Otherwise use a random number. * */ private static String getVisitorId(String account, String userAgent, MessageContext msgCtx) throws NoSuchAlgorithmException, UnsupportedEncodingException { if (msgCtx.getProperty(COOKIE_NAME) != null) { return (String) msgCtx.getProperty(COOKIE_NAME); } String message; AuthenticationContext authContext = APISecurityUtils.getAuthenticationContext(msgCtx); if (authContext != null) { message = authContext.getApiKey(); } else { message = ANONYMOUS_USER_ID; } MessageDigest m = MessageDigest.getInstance("MD5"); m.update(message.getBytes("UTF-8"), 0, message.length()); byte[] sum = m.digest(); BigInteger messageAsNumber = new BigInteger(1, sum); String md5String = messageAsNumber.toString(16); /* Pad to make sure id is 32 characters long. */ while (md5String.length() < 32) { md5String = "0" + md5String; } return "0x" + md5String.substring(0, 16); }
From source file:tw.com.ksmt.cloud.libs.Utils.java
public static String getMD5(String input) { try {/*from w w w. jav a 2 s. c o m*/ MessageDigest md = MessageDigest.getInstance("MD5"); byte[] messageDigest = md.digest(input.getBytes()); BigInteger number = new BigInteger(1, messageDigest); String md5 = number.toString(16); while (md5.length() < 32) { md5 = "0" + md5; } return md5; } catch (NoSuchAlgorithmException e) { Log.e("MD5", e.getLocalizedMessage()); return null; } }
From source file:org.deri.pipes.utils.HttpResponseCache.java
/** * @param string//from w w w . j ava2s .c o m * @return */ static String getMD5(String string) { try { MessageDigest digest = MessageDigest.getInstance("MD5"); BigInteger bigInt = new BigInteger(1, digest.digest(string.getBytes("UTF-8"))); String md5 = bigInt.toString(16); if (logger.isDebugEnabled()) { logger.debug("using md5=[" + md5 + "] for " + string); } return md5; } catch (Throwable t) { logger.info("couldn't calculate md5 because:" + t, t); return string; } }
From source file:com.cloud.test.utils.UtilsForTest.java
public static String createMD5String(String password) { MessageDigest md5;/*from w ww. j a v a2 s. c o m*/ try { md5 = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new CloudRuntimeException("Error", e); } md5.reset(); BigInteger pwInt = new BigInteger(1, md5.digest(password.getBytes())); // make sure our MD5 hash value is 32 digits long... StringBuffer sb = new StringBuffer(); String pwStr = pwInt.toString(16); int padding = 32 - pwStr.length(); for (int i = 0; i < padding; i++) { sb.append('0'); } sb.append(pwStr); return sb.toString(); }
From source file:cz.muni.fi.airportservicelayer.services.StewardServiceImpl.java
private static String toHex(byte[] array) { BigInteger bi = new BigInteger(1, array); String hex = bi.toString(16); int paddingLength = (array.length * 2) - hex.length(); return paddingLength > 0 ? String.format("%0" + paddingLength + "d", 0) + hex : hex; }