Example usage for org.bouncycastle.util.encoders Base64Encoder Base64Encoder

List of usage examples for org.bouncycastle.util.encoders Base64Encoder Base64Encoder

Introduction

In this page you can find the example usage for org.bouncycastle.util.encoders Base64Encoder Base64Encoder.

Prototype

public Base64Encoder() 

Source Link

Usage

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  w w w. ja va2  s  .c  om*/
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

/**
 * Signs a package file//w ww.  j av  a 2 s.com
 * 
 * @param packageFile
 *            the package file to sign
 * @param certificateAlias
 *            the signing certificate alias
 * @param createdBy
 *            Created-By manifest attribute
 * @throws SignException
 *             if a processing error occurs during the signing process
 * @throws UnrecoverableKeyException 
 */
public static void signPackage(PackageFile packageFile, IKeyStoreEntry keystoreEntry, String keyEntryPassword,
        String createdBy) throws SignException, UnrecoverableKeyException {

    try {
        Base64Encoder encoder = new Base64Encoder();
        MessageDigest messageDigest = MessageDigest.getInstance(ISignConstants.SHA1);

        addFilesDigestsToManifest(packageFile, encoder, messageDigest);

        addSignatureFiles(packageFile, keystoreEntry, keyEntryPassword, encoder, createdBy);
    } catch (UnrecoverableKeyException e) {
        throw e;
    } catch (Exception e) {
        StudioLogger.error(PackageFileSigner.class, "Error signing package", e);
        throw new SignException(e.getMessage(), e);
    }

}

From source file:com.nokia.xfolite.xforms.xpath.XFormsCoreFunctionLibrary.java

License:Open Source License

public static String digest_function(String s, String method, String encoding) {
    //#debug info
    System.out.println("Digest function invoked with params(" + s + "," + method + "," + encoding + ")");
    Digest digest = null;/*from w  w w.j  a  v  a2s .c om*/
    Encoder encoder = null;
    if (encoding.equals("base64")) {
        encoder = new Base64Encoder();
    } else if (encoding.equals("hex")) {
        encoder = new HexEncoder();
    }
    if (method.equals("SHA-1")) {
        digest = new SHA1Digest();
    } else if (method.equals("SHA-256")) {
        digest = new SHA256Digest();
    } else if (method.equals("SHA-512")) {
        digest = new SHA512Digest();
    } else if (method.equals("MD5")) {
        digest = new MD5Digest();
    }

    if (encoder == null) {
        throw new XPathException(XPathException.TYPE_ERR,
                "XForms function digest() only supports hex and base64 encoding.");
    }
    if (digest == null) {
        throw new XPathException(XPathException.TYPE_ERR,
                "XForms function digest() only supports MD5, SHA-1, SHA-256 and SHA-512 digests.");
    }

    int len = s.length();
    for (int i = 0; i < len; i++) {
        digest.update((byte) s.charAt(i)); // FIXME: Better not use non-ASCII characters!
    }

    byte[] data = new byte[digest.getDigestSize()];
    digest.doFinal(data, 0);

    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    try {
        encoder.encode(data, 0, data.length, bOut);
    } catch (IOException e) {
        throw new XPathException(XPathException.TYPE_ERR, "Exception when encoding digest: " + e);
    }

    byte[] out = bOut.toByteArray();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < out.length; i++) {
        sb.append((char) out[i]); // This works fine, neither hex nor base64 encodings produce 
    }
    return sb.toString();
}

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 w  w  w  .  j a  va2s.c  om*/
 */
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   w  w w .  j  a  v a 2s . 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

/**
 * Signs a package file//w  w w. j a  va  2s  .co  m
 * 
 * @param packageFile
 *            the package file to sign
 * @param certificateAlias
 *            the signing certificate alias
 * @param createdBy
 *            Created-By manifest attribute
 * @throws SignException
 *             if a processing error occurs during the signing process
 * @throws UnrecoverableKeyException
 */
public static void signPackage(PackageFile packageFile, IKeyStoreEntry keystoreEntry, String keyEntryPassword,
        String createdBy) throws SignException, UnrecoverableKeyException {

    try {
        Base64Encoder encoder = new Base64Encoder();
        MessageDigest messageDigest = MessageDigest.getInstance(ISignConstants.SHA1);

        addFilesDigestsToManifest(packageFile, encoder, messageDigest);

        addSignatureFiles(packageFile, keystoreEntry, keyEntryPassword, encoder, createdBy);
    } catch (UnrecoverableKeyException e) {
        throw e;
    } catch (Exception e) {
        AndmoreLogger.error(PackageFileSigner.class, "Error signing package", e);
        throw new SignException(e.getMessage(), e);
    }

}

From source file:org.jitsi.videobridge.stats.callstats.TokenGenerator.java

License:Apache License

/**
 * Returns the private key to use./*w  ww  . j  a va2s.c  o m*/
 * @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.openoces.opensign.demo.utils.Base64.java

License:Open Source License

public static String base64Encode(byte[] bytes) {
    try {//from   w w w. j a v  a  2 s  .c  o  m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new Base64Encoder().encode(bytes, 0, bytes.length, baos);
        return baos.toString();
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.sonatype.nexus.crypto.internal.PasswordCipher.java

License:Open Source License

public PasswordCipher(final CryptoHelper cryptoHelper) {
    this.cryptoHelper = checkNotNull(cryptoHelper);
    this.base64Encoder = new Base64Encoder();
    this.secureRandom = cryptoHelper.createSecureRandom();
    this.secureRandom.setSeed(System.nanoTime());
}

From source file:org.sonatype.nexus.crypto.maven.PasswordCipherMavenImpl.java

License:Open Source License

public PasswordCipherMavenImpl(final CryptoHelper cryptoHelper) {
    this.cryptoHelper = checkNotNull(cryptoHelper);
    this.base64Encoder = new Base64Encoder();
    this.secureRandom = cryptoHelper.createSecureRandom();
    this.secureRandom.setSeed(System.nanoTime());
}