fabiomsr / okhttp-peer-certificate-extractor

This tool extracts peer certificates from given certificates.
Apache License 2.0
79 stars 19 forks source link

How to import java.util.Base64 to Android project? #2

Open roblav96 opened 7 years ago

roblav96 commented 7 years ago

Currently my app crashes due to java.util.Base64 missing. I think it should be android.util.Base64?

https://github.com/fabiomsr/okhttp-peer-certificate-extractor/blob/master/src/main/java/org/fabiomsr/peercertificate/PeerCertificateExtractor.java#L11

fabiomsr commented 7 years ago

I think it should be android.util.Base64?

Yes.

You should remove these lines.

 byte[] publicKeyShaBase64 = Base64.getEncoder().encode(publicKeySha256);
return "sha256/" + new String(publicKeyShaBase64);

And add:

return "sha256/" + Base64.encodeToString(publicKeySha256, Base64.DEFAULT);
roblav96 commented 7 years ago

@fabiomsr I found out that using okhttp3 you can call the method okhttp3.CertificatePinner.pin(x509Certificate) for example:

let inputStream: java.io.FileInputStream
try {
    let file = new java.io.File(options.certificateURI)
    inputStream = new java.io.FileInputStream(file)
    let x509Certificate = java.security.cert.CertificateFactory.getInstance('X509').generateCertificate(inputStream)
    peer.x509Certificate = x509Certificate
    certificate = okhttp3.CertificatePinner.pin(x509Certificate)
    inputStream.close()
} catch (error) {
    try {
        if (inputStream) {
            inputStream.close()
        }
    } catch (e) { }
    console.error('nativescript-https > enableSSLPinning error', error)
    return
}
// certificate = sha256/u4QJrwx7aSejc080BBQKGyTaoJovFBg4SbQ9nhoohb8=

Used in this library I wrote for Nativescript https://github.com/gethuman/nativescript-https/blob/master/https.android.ts#L41