List of usage examples for java.math BigInteger toString
public String toString(int radix)
From source file:com.cloud.server.auth.MD5UserAuthenticator.java
@Override public String encode(final String password) { try {/* w w w .ja v a2s . c o m*/ final MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.reset(); BigInteger pwInt = new BigInteger(1, md5.digest(password.getBytes())); String pwStr = pwInt.toString(16); int padding = 32 - pwStr.length(); StringBuilder sb = new StringBuilder(32); for (int i = 0; i < padding; i++) { sb.append('0'); // make sure the MD5 password is 32 digits long } sb.append(pwStr); return sb.toString(); } catch (NoSuchAlgorithmException e) { throw new CloudRuntimeException("Unable to hash password", e); } }
From source file:grails.plugin.springsecurity.authentication.encoding.PBKDF2PasswordEncoder.java
/** * Converts a byte array into a hexadecimal string. * * @param array the byte array to convert * @return a length*2 character string encoding the byte array *//*from ww w. j a va2 s. co m*/ protected String toHex(byte[] array) { BigInteger bi = new BigInteger(1, array); String hex = bi.toString(16); int paddingLength = (array.length * 2) - hex.length(); if (paddingLength > 0) { return String.format("%0" + paddingLength + "d", 0) + hex; } return hex; }
From source file:org.opendatakit.common.android.utilities.ODKFileUtils.java
public static String getNakedMd5Hash(String appName, File file) { try {/* w w w. jav a 2 s . c o m*/ // CTS (6/15/2010) : stream file through digest instead of handing // it the byte[] MessageDigest md = MessageDigest.getInstance("MD5"); int chunkSize = 8192; byte[] chunk = new byte[chunkSize]; // Get the size of the file long lLength = file.length(); if (lLength > Integer.MAX_VALUE) { WebLogger.getLogger(appName).e(t, "File " + file.getName() + "is too large"); return null; } int length = (int) lLength; InputStream is = null; is = new FileInputStream(file); int l = 0; for (l = 0; l + chunkSize < length; l += chunkSize) { is.read(chunk, 0, chunkSize); md.update(chunk, 0, chunkSize); } int remaining = length - l; if (remaining > 0) { is.read(chunk, 0, remaining); md.update(chunk, 0, remaining); } byte[] messageDigest = md.digest(); BigInteger number = new BigInteger(1, messageDigest); String md5 = number.toString(16); while (md5.length() < 32) md5 = "0" + md5; is.close(); return md5; } catch (NoSuchAlgorithmException e) { WebLogger.getLogger(appName).e("MD5", e.getMessage()); return null; } catch (FileNotFoundException e) { WebLogger.getLogger(appName).e("No Cache File", e.getMessage()); return null; } catch (IOException e) { WebLogger.getLogger(appName).e("Problem reading from file", e.getMessage()); return null; } }
From source file:org.opendatakit.common.android.utilities.ODKFileUtils.java
public static String getNakedMd5Hash(String appName, String contents) { try {/*w w w . j av a 2 s.c o m*/ // CTS (6/15/2010) : stream file through digest instead of handing // it the byte[] MessageDigest md = MessageDigest.getInstance("MD5"); int chunkSize = 256; byte[] chunk = new byte[chunkSize]; // Get the size of the file long lLength = contents.length(); if (lLength > Integer.MAX_VALUE) { WebLogger.getLogger(appName).e(t, "Contents is too large"); return null; } int length = (int) lLength; InputStream is = null; is = new ByteArrayInputStream(contents.getBytes(CharEncoding.UTF_8)); int l = 0; for (l = 0; l + chunkSize < length; l += chunkSize) { is.read(chunk, 0, chunkSize); md.update(chunk, 0, chunkSize); } int remaining = length - l; if (remaining > 0) { is.read(chunk, 0, remaining); md.update(chunk, 0, remaining); } byte[] messageDigest = md.digest(); BigInteger number = new BigInteger(1, messageDigest); String md5 = number.toString(16); while (md5.length() < 32) md5 = "0" + md5; is.close(); return md5; } catch (NoSuchAlgorithmException e) { WebLogger.getLogger(appName).e("MD5", e.getMessage()); return null; } catch (FileNotFoundException e) { WebLogger.getLogger(appName).e("No Cache File", e.getMessage()); return null; } catch (IOException e) { WebLogger.getLogger(appName).e("Problem reading from file", e.getMessage()); return null; } }
From source file:mitm.common.security.certificate.GenerateKeyPairs.java
private String bigIntToString(BigInteger bigInt) throws IOException { return FixedWidthReader.fixedWidth(bigInt.toString(16), WIDTH); }
From source file:org.alfresco.repo.virtual.ref.NodeRefRadixHasher.java
@Override public NodeRef lookup(Pair<String, String> hash) { String storeHash = hash.getFirst(); String storeProtocolHash = storeHash.substring(0, 1); String storeIdHash = storeHash.substring(1, 2); String storeProtocol = storeProtocolStore.lookup(storeProtocolHash); String storeId = storeIdStore.lookup(storeIdHash); if (storeProtocol == null || storeId == null) { throw new RuntimeException("Lookup found no protocol or id for " + storeHash); }/*from ww w.j a v a 2s . co m*/ BigInteger nodeId = new BigInteger(hash.getSecond(), radix); String nodeIdHexa = nodeId.toString(16); nodeIdHexa = StringUtils.leftPad(nodeIdHexa, 32, "0"); int leadZeros = 32 - nodeIdHexa.length(); if (leadZeros > 0) { } String groups[] = new String[5]; groups[0] = nodeIdHexa.substring(0, 8); groups[1] = nodeIdHexa.substring(8, 12); groups[2] = nodeIdHexa.substring(12, 16); groups[3] = nodeIdHexa.substring(16, 20); groups[4] = nodeIdHexa.substring(20, 32); StringBuilder idBuilder = new StringBuilder(groups[0]); for (int i = 1; i < groups.length; i++) { idBuilder.append("-"); idBuilder.append(groups[i]); } return new NodeRef(storeProtocol, storeId, idBuilder.toString()); }
From source file:nl.minvenj.pef.pseudo.IPPseudonymizer.java
private byte[] pseudonymize(final byte[] ipAddress, final int bitCount, final int mask, final int changeBitCount) { final BigInteger ipBigInt = new BigInteger(1, ipAddress); final String ipString = ipBigInt.toString(RADIX); final BigInteger bigIntMask = BigInteger.ONE.shiftLeft(bitCount - mask).subtract(BigInteger.ONE); final String bitStringToEncrypt = ipBigInt.and(bigIntMask).toString(RADIX); final String bitStringToEncryptPadded = Util.padZeroLeft(bitStringToEncrypt, changeBitCount - bitStringToEncrypt.length()); final String encrypted = _encrypter.encrypt(TWEAK, bitStringToEncryptPadded); final int keptBitCount = ipString.length() - changeBitCount; // keptBitCount is how many (string) bits to keep there are left in the original string // this is not necessarily equal to the mask, due to the conversion from BigInteger // since it will get converted to a number, take those left and concat, or if none, just use encrypted final String rebuiltBitString = keptBitCount > 0 ? ipString.substring(0, keptBitCount).concat(encrypted) : encrypted;// w w w. j a v a2s . co m return toIPAddressOfSize(new BigInteger(rebuiltBitString, RADIX), ipAddress.length); }
From source file:ro.kuberam.libs.java.crypto.CryptoModuleTests.java
@Test public void digestOutputStreamTest() throws Exception { final MessageDigest md = MessageDigest.getInstance("SHA-512"); try (final OutputStream fos = Files.newOutputStream(Paths .get("/home/claudius/workspace-claudius/expath-crypto/src/org/expath/crypto/tests/string.txt")); final DigestOutputStream dos = new DigestOutputStream(fos, md); final ObjectOutputStream oos = new ObjectOutputStream(dos)) { final String data = "This have I thought good to deliver thee, " + "that thou mightst not lose the dues of rejoicing " + "by being ignorant of what greatness is promised thee."; oos.writeObject(data);/*w w w . ja v a 2s . c o m*/ dos.on(false); final byte[] digest = md.digest(); oos.writeObject(digest); final int digestLength = digest.length; System.out.println("length: " + digestLength); final BigInteger bi = new BigInteger(1, digest); String result = bi.toString(digestLength); if (result.length() % 2 != 0) { result = "0" + result; } System.out.println("result: " + result); } }
From source file:org.rifidi.edge.adapter.awid.awid2010.communication.messages.Gen2ReadBlockDataResponse.java
@Override public TagReadEvent getTagReadEvent() { EPCGeneration2Event gen2Event = new EPCGeneration2Event(); byte[] shiftedmessage = new byte[super.rawmessage.length]; BigInteger bi = new BigInteger(super.rawmessage); BigInteger bi2 = bi.shiftLeft(1); shiftedmessage = bi2.toByteArray();/*from w w w. ja v a2 s .c o m*/ if (memorybank == 0) { logger.debug("Reserved Memory Bank" + new String(Hex.encodeHex(shiftedmessage))); //TODO: put bytes in reserved mem bank } else if (memorybank == 1) { logger.debug("EPC Memory Bank" + new String(Hex.encodeHex(shiftedmessage))); //TODO: what if we don't have 96 bits? BigInteger epc = new BigInteger(Arrays.copyOfRange(shiftedmessage, 7, 19)); gen2Event.setEPCMemory(epc, epc.toString(16), 12 * 8); } else if (memorybank == 2) { logger.debug("TID Memory Bank " + new String(Hex.encodeHex(shiftedmessage))); // TODO: this is not quite correct since it assumes that we have 96 // bits gen2Event.setTIDMemory(new BigInteger(Arrays.copyOfRange(shiftedmessage, 3, 17)), 12 * 8); } else if (memorybank == 3) { logger.debug("User Memory Bank" + new String(Hex.encodeHex(shiftedmessage))); // TODO: insert into user memory } TagReadEvent tre; if (super.rawmessage.length == 22) { logger.debug("AntennaID " + super.rawmessage[19]); tre = new TagReadEvent(readerID, gen2Event, super.rawmessage[19], timestamp); } else { logger.warn("No Antenna byte, assuming default value of " + 1); tre = new TagReadEvent(readerID, gen2Event, 1, timestamp); } return tre; }
From source file:org.dataconservancy.dcs.integration.main.IngestTest.java
public String hashPassword(String password) { String hashword = null;//from w w w.ja v a 2 s . c o m try { MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(password.getBytes()); BigInteger hash = new BigInteger(1, md5.digest()); hashword = hash.toString(16); } catch (NoSuchAlgorithmException localNoSuchAlgorithmException) { } return pad(hashword, 32, '0'); }