List of usage examples for org.bouncycastle.util.encoders Base64Encoder encode
public int encode(byte[] data, int off, int length, OutputStream out) throws IOException
From source file:com.motorolamobility.studio.android.certmanager.packaging.sign.ManifestEntry.java
License:Apache License
/** * Get this entry ready to be written in the Signature File * @return this entry ready to be written in the Signature File * @throws IOException if some error occurs during encoding *///from ww w . ja va2s . c o m public String toDigestedManifestEntry() throws IOException { Base64Encoder encoder = new Base64Encoder(); StringBuilder builder = new StringBuilder(); ByteArrayOutputStream output = null; try { output = new ByteArrayOutputStream(); builder.append(wrap72bytes(ENTRY_NAME_ATTRIBUTE + name)); builder.append(MANIFEST_NEW_LINE); builder.append(ISignConstants.SHA1_DIGEST + ": "); byte[] digest = digest(); encoder.encode(digest, 0, digest.length, output); builder.append(output.toString()); builder.append(MANIFEST_NEW_LINE); } finally { if (output != null) { try { output.close(); } catch (IOException e) { StudioLogger.error("Could not close stream: " + e.getMessage()); } } } return builder.toString(); }
From source file:com.motorolamobility.studio.android.certmanager.packaging.sign.PackageFileSigner.java
License:Apache License
/** * Generates the digests for all the files in the package and puts them in * the manifest//from w w w . j a va 2 s . c om * * @param packageFile * the package file being signed * @param encoder * the BASE64 encoder * @param messageDigest * the message digest * @throws IOException * if an I/O error occurs when reading the files contained in * the package */ private static void addFilesDigestsToManifest(PackageFile packageFile, Base64Encoder encoder, MessageDigest messageDigest) throws IOException { InputStream fileInputStream = null; ReadableByteChannel rc = null; ByteArrayOutputStream encodedStream = null; // for each entry in the package file for (String entryName : packageFile.getEntryNames()) { File file = packageFile.getEntryFile(entryName); if (file.isFile()) { try { // read the file contents fileInputStream = new FileInputStream(file); rc = Channels.newChannel(fileInputStream); ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length()); rc.read(byteBuffer); // compute the digest messageDigest.reset(); byte[] digestedArray = messageDigest.digest(byteBuffer.array()); encodedStream = new ByteArrayOutputStream(); encoder.encode(digestedArray, 0, digestedArray.length, encodedStream); String digestedMessage = encodedStream.toString(); // put the digest in the manifest file Attributes jarEntryAttributes = new Attributes(); jarEntryAttributes.putValue(ISignConstants.SHA1_DIGEST, digestedMessage); packageFile.getManifest().getEntries().put(entryName, jarEntryAttributes); } finally { try { if (encodedStream != null) { encodedStream.close(); } if (rc != null) { rc.close(); } if (fileInputStream != null) { fileInputStream.close(); } } catch (IOException e) { StudioLogger.error("Could not close stream while signing package. " + e.getMessage()); } } } } }
From source file:net.ripe.rpki.commons.crypto.x509cert.X509CertificateUtil.java
License:BSD License
/** * Get a base 64-encoded, DER-encoded X.509 subjectPublicKeyInfo as used for the Trust Anchor Locator (TAL) * * @throws X509CertificateOperationException * * @throws IOException/*from ww w .j ava 2 s . co m*/ */ public static String getEncodedSubjectPublicKeyInfo(X509Certificate certificate) { byte[] tbsCertificate; try { tbsCertificate = certificate.getTBSCertificate(); } catch (CertificateEncodingException e) { throw new X509CertificateOperationException("Can't extract TBSCertificate from certificate", e); } ASN1Sequence tbsCertificateSequence = (ASN1Sequence) Asn1Util.decode(tbsCertificate); TBSCertificateStructure tbsCertificateStructure = new TBSCertificateStructure(tbsCertificateSequence); SubjectPublicKeyInfo subjectPublicKeyInfo = tbsCertificateStructure.getSubjectPublicKeyInfo(); try { byte[] data = subjectPublicKeyInfo.getEncoded(); Base64Encoder encoder = new Base64Encoder(); ByteArrayOutputStream out = new ByteArrayOutputStream(); encoder.encode(data, 0, data.length, out); out.flush(); return out.toString(); } catch (IOException e) { throw new X509CertificateOperationException("Can't encode SubjectPublicKeyInfo for certificate", e); } }
From source file:org.eclipse.andmore.android.certmanager.packaging.sign.ManifestEntry.java
License:Apache License
/** * Get this entry ready to be written in the Signature File * /*from ww w . jav a2 s . c o m*/ * @return this entry ready to be written in the Signature File * @throws IOException * if some error occurs during encoding */ public String toDigestedManifestEntry() throws IOException { Base64Encoder encoder = new Base64Encoder(); StringBuilder builder = new StringBuilder(); ByteArrayOutputStream output = null; try { output = new ByteArrayOutputStream(); builder.append(wrap72bytes(ENTRY_NAME_ATTRIBUTE + name)); builder.append(MANIFEST_NEW_LINE); builder.append(ISignConstants.SHA1_DIGEST + ": "); byte[] digest = digest(); encoder.encode(digest, 0, digest.length, output); builder.append(output.toString()); builder.append(MANIFEST_NEW_LINE); } finally { if (output != null) { try { output.close(); } catch (IOException e) { AndmoreLogger.error("Could not close stream: " + e.getMessage()); } } } return builder.toString(); }
From source file:org.eclipse.andmore.android.certmanager.packaging.sign.PackageFileSigner.java
License:Apache License
/** * Generates the digests for all the files in the package and puts them in * the manifest//from w ww. j a v a 2 s .co m * * @param packageFile * the package file being signed * @param encoder * the BASE64 encoder * @param messageDigest * the message digest * @throws IOException * if an I/O error occurs when reading the files contained in * the package */ private static void addFilesDigestsToManifest(PackageFile packageFile, Base64Encoder encoder, MessageDigest messageDigest) throws IOException { InputStream fileInputStream = null; ReadableByteChannel rc = null; ByteArrayOutputStream encodedStream = null; // for each entry in the package file for (String entryName : packageFile.getEntryNames()) { File file = packageFile.getEntryFile(entryName); if (file.isFile()) { try { // read the file contents fileInputStream = new FileInputStream(file); rc = Channels.newChannel(fileInputStream); ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length()); rc.read(byteBuffer); // compute the digest messageDigest.reset(); byte[] digestedArray = messageDigest.digest(byteBuffer.array()); encodedStream = new ByteArrayOutputStream(); encoder.encode(digestedArray, 0, digestedArray.length, encodedStream); String digestedMessage = encodedStream.toString(); // put the digest in the manifest file Attributes jarEntryAttributes = new Attributes(); jarEntryAttributes.putValue(ISignConstants.SHA1_DIGEST, digestedMessage); packageFile.getManifest().getEntries().put(entryName, jarEntryAttributes); } finally { try { if (encodedStream != null) { encodedStream.close(); } if (rc != null) { rc.close(); } if (fileInputStream != null) { fileInputStream.close(); } } catch (IOException e) { AndmoreLogger.error("Could not close stream while signing package. " + e.getMessage()); } } } } }
From source file:org.jitsi.videobridge.stats.callstats.TokenGenerator.java
License:Apache License
/** * Returns the private key to use.//ww w . j a v a 2s . com * @param keyPath the path to the key * @return the private key. * @throws IOException error reading file * @throws JoseException error parsing file */ private static PrivateKey readPrivateKey(String keyPath) throws IOException, JoseException { Type mapType = new TypeToken<Map<String, String>>() { }.getType(); Map<String, String> son = new Gson().fromJson(new FileReader(keyPath), mapType); EllipticCurveJsonWebKey jwk = new EllipticCurveJsonWebKey((Map<String, Object>) (Map) son); Base64Encoder enc = new Base64Encoder(); ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] encodedKey = jwk.getPrivateKey().getEncoded(); enc.encode(encodedKey, 0, encodedKey.length, os); return jwk.getPrivateKey(); }
From source file:org.sonatype.security.ldap.upgrade.cipher.DefaultPlexusCipher.java
License:Open Source License
public String encrypt(String str, String passPhrase) throws PlexusCipherException { try {//from w w w . j a va 2s. c o m byte[] salt = getSalt(SALT_SIZE); Cipher cipher = init(passPhrase, salt, true); // Encode the string into bytes using utf-8 byte[] utf8 = str.getBytes(STRING_ENCODING); // Encrypt it byte[] enc = cipher.doFinal(utf8); // Encode bytes to base64 to get a string Base64Encoder b64 = new Base64Encoder(); byte saltLen = (byte) (salt.length & 0x00ff); int encLen = enc.length; byte[] res = new byte[salt.length + encLen + 1]; res[0] = saltLen; System.arraycopy(salt, 0, res, 1, saltLen); System.arraycopy(enc, 0, res, saltLen + 1, encLen); ByteArrayOutputStream bout = new ByteArrayOutputStream(res.length * 2); b64.encode(res, 0, res.length, bout); return bout.toString(STRING_ENCODING); } catch (Exception e) { throw new PlexusCipherException(e); } }