List of usage examples for java.math BigInteger toByteArray
public byte[] toByteArray()
From source file:org.apli.modelbeans.facturacion.cfdi.CFDv32.java
@Override public void sellar(PrivateKey key, X509Certificate cert) throws Exception { cert.checkValidity();//from www. j a v a 2 s. c o m String signature = getSignature(key); document.setSello(signature); byte[] bytes = cert.getEncoded(); Base64 b64 = new Base64(-1); String certStr = b64.encodeToString(bytes); document.setCertificado(certStr); BigInteger bi = cert.getSerialNumber(); document.setNoCertificado(new String(bi.toByteArray())); }
From source file:org.cloudfoundry.identity.uaa.oauth.jwk.RsaJsonWebKeyTests.java
@Test public void create_key_from_pem_string() { Base64 base64 = new Base64(true); KeyInfo keyInfo = new KeyInfo(); keyInfo.setKeyId("id"); keyInfo.setSigningKey(sampleRsaPrivateKey); assertEquals("RSA", keyInfo.getType()); assertNotNull(keyInfo.getVerifier()); PublicKey pk = keyInfo.getRsaPublicKey(); JsonWebKey key = JsonWebKeyHelper.fromPEMPrivateKey(keyInfo.getVerifierKey()).setKid("id"); assertEquals(RSA, key.getKty());/* ww w.j av a2 s . com*/ assertEquals("RSA", key.getKeyProperties().get("kty")); assertEquals("id", key.getKid()); assertEquals(sig, key.getUse()); assertEquals("sig", key.getKeyProperties().get("use")); assertNotNull(key.getValue()); BigInteger exponent = ((RSAPublicKey) pk).getPublicExponent(); BigInteger modulus = ((RSAPublicKey) pk).getModulus(); assertEquals(base64.encodeAsString(exponent.toByteArray()), key.getKeyProperties().get("e")); assertEquals(base64.encodeAsString(modulus.toByteArray()), key.getKeyProperties().get("n")); }
From source file:org.cloudfoundry.identity.uaa.oauth.jwk.RsaJsonWebKeyTests.java
@Test public void create_key_from_public_pem_string() { Base64 base64 = new Base64(true); KeyInfo keyInfo = new KeyInfo(); keyInfo.setKeyId("id"); keyInfo.setSigningKey(sampleRsaPrivateKey); assertEquals("RSA", keyInfo.getType()); assertNotNull(keyInfo.getVerifier()); PublicKey pk = keyInfo.getRsaPublicKey(); JsonWebKey key = JsonWebKeyHelper.fromPEMPublicKey(KeyInfo.pemEncodePublicKey(pk)).setKid("id"); assertEquals(RSA, key.getKty());// www. j a v a 2s. c om assertEquals("RSA", key.getKeyProperties().get("kty")); assertEquals("id", key.getKid()); assertEquals(sig, key.getUse()); assertEquals("sig", key.getKeyProperties().get("use")); assertNotNull(key.getValue()); BigInteger exponent = ((RSAPublicKey) pk).getPublicExponent(); BigInteger modulus = ((RSAPublicKey) pk).getModulus(); assertEquals(base64.encodeAsString(exponent.toByteArray()), key.getKeyProperties().get("e")); assertEquals(base64.encodeAsString(modulus.toByteArray()), key.getKeyProperties().get("n")); }
From source file:uk.ac.cam.cl.dtg.segue.auth.SegueLocalAuthenticator.java
/** * Hash the password using the preconfigured hashing function. * * @param password/* ww w .j a v a2 s . com*/ * - password to hash * @param salt * - random string to use as a salt. * @return the Base64 encoded hashed password * @throws NoSuchAlgorithmException * - if the configured algorithm is not valid. * @throws InvalidKeySpecException * - if the preconfigured key spec is invalid. */ private String hashPassword(final String password, final String salt) throws NoSuchAlgorithmException, InvalidKeySpecException { BigInteger hashedPassword = new BigInteger(computeHash(password, salt, KEY_LENGTH)); return new String(Base64.encodeBase64(hashedPassword.toByteArray())); }
From source file:com.uit.anonymousidentity.Repository.Nonces.NonceJDBCTemplate.java
@Override public Nonce find(BigInteger i) throws SQLException { String sql = "select * from " + TABLE_NAME + " where " + VALUE + " = ?"; PreparedStatement pst = dataSource.getConnection().prepareStatement(sql); Nonce n = new Nonce(); pst.setBytes(1, i.toByteArray()); ResultSet rs = pst.executeQuery(); if (rs.next()) { n.setIssuerSid(rs.getString(SID)); n.setByteArray(rs.getBytes(VALUE)); n.setId(rs.getInt(ID));//w ww . ja v a2 s. co m return n; } else return null; }
From source file:com.archivas.clienttools.arcutils.utils.net.SSLCertChain.java
public String getSerialNumberString() { X509Certificate cert = getCertificateList().get(0); BigInteger sn = cert.getSerialNumber(); return byteArrayToColonSeparatedHexString(sn.toByteArray(), ":"); }
From source file:uk.org.ukfederation.mda.validate.X509RSAOpenSSLBlacklistValidator.java
/** * Computes the OpenSSL digest value for the given modulus. * // ww w . j a va2 s .co m * @param modulus RSA public modulus to be digested * @return value to be compared against the blacklist * @throws StageProcessingException if SHA1 digester can not be acquired, or for internal * errors related to {@link ByteArrayOutputStream} */ @Nonnull private String openSSLDigest(@Nonnull final BigInteger modulus) throws StageProcessingException { try { // Acquire a representation of the modulus byte[] modulusBytes = modulus.toByteArray(); if (modulusBytes[0] == 0) { // drop first 00 byte of modulus representation modulusBytes = Arrays.copyOfRange(modulusBytes, 1, modulusBytes.length); } // Encode the modulus into upper-case hex characters final char[] encodedModulus = Hex.encodeHex(modulusBytes, false); // Now construct the thing we want to hash ByteArrayOutputStream bb = new ByteArrayOutputStream(); try { bb.write(openSSLprefix); for (char c : encodedModulus) { bb.write((byte) c); } bb.write('\n'); } catch (IOException e) { throw new StageProcessingException("internal error writing to ByteArrayStream", e); } //System.out.println("To be digested: " + bb.toString()); // Make the digest final MessageDigest digest = MessageDigest.getInstance("SHA1"); digest.update(bb.toByteArray()); final byte[] bytes = digest.digest(); // Convert the digest to a lower-case hex string char[] encodedDigest = Hex.encodeHex(bytes, true); final String strValue = String.valueOf(encodedDigest); final String trimmed = strValue.substring(20); //System.out.println("Digest: " + strValue + " trimmed " + trimmed); return trimmed; } catch (NoSuchAlgorithmException e) { throw new StageProcessingException("could not create message digester", e); } }
From source file:com.dulion.astatium.mesh.shredder.ContextManager.java
private void logTree() { String space = Strings.repeat(" ", 40); Context[] array = new Context[contextMap.size()]; contextMap.values().toArray(array);//from w w w . j a va 2 s . c o m sort(array, Context.byContext()); int maxSize = 0; for (Context context : array) { Location location = context.getRange().getLower(); BigInteger numerator = ((RationalLocation) location).getNumerator(); String num = Base64.getEncoder().encodeToString(numerator.toByteArray()); int numSize = numerator.bitLength() / 8 + 1; if (numSize > maxSize) { maxSize = numSize; } BigInteger denominator = ((RationalLocation) location).getDenominator(); String den = Base64.getEncoder().encodeToString(denominator.toByteArray()); int denSize = denominator.bitLength() / 8 + 1; if (denSize > maxSize) { maxSize = denSize; } int depth = context.getDepth() - 1; String indent = space.substring(0, depth << 1); LOG.info(String.format("[%15s|%15s]%s", num, den, indent + context.getName())); } LOG.info("Max byte size: {}", maxSize); }
From source file:Main.java
public static byte[] decodeBase58(String input) { if (input == null) { return null; }/*from w w w. ja v a 2 s .c om*/ input = input.trim(); if (input.length() == 0) { return new byte[0]; } BigInteger resultNum = BigInteger.ZERO; int nLeadingZeros = 0; while (nLeadingZeros < input.length() && input.charAt(nLeadingZeros) == BASE58[0]) { nLeadingZeros++; } long acc = 0; int nDigits = 0; int p = nLeadingZeros; while (p < input.length()) { int v = BASE58_VALUES[input.charAt(p) & 0xff]; if (v >= 0) { acc *= 58; acc += v; nDigits++; if (nDigits == BASE58_CHUNK_DIGITS) { resultNum = resultNum.multiply(BASE58_CHUNK_MOD).add(BigInteger.valueOf(acc)); acc = 0; nDigits = 0; } p++; } else { break; } } if (nDigits > 0) { long mul = 58; while (--nDigits > 0) { mul *= 58; } resultNum = resultNum.multiply(BigInteger.valueOf(mul)).add(BigInteger.valueOf(acc)); } final int BASE58_SPACE = -2; while (p < input.length() && BASE58_VALUES[input.charAt(p) & 0xff] == BASE58_SPACE) { p++; } if (p < input.length()) { return null; } byte[] plainNumber = resultNum.toByteArray(); int plainNumbersOffs = plainNumber[0] == 0 ? 1 : 0; byte[] result = new byte[nLeadingZeros + plainNumber.length - plainNumbersOffs]; System.arraycopy(plainNumber, plainNumbersOffs, result, nLeadingZeros, plainNumber.length - plainNumbersOffs); return result; }