Code example for PKCS8EncodedKeySpec

Methods: sleep

0
        return getCurrentHash(content);
    } 
 
    private PrivateKey createKey() throws Exception {
        byte[] derKey = Base64.decode(TEST_KEY.getBytes(), Base64.DEFAULT);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(derKey);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return (PrivateKey) keyFactory.generatePrivate(keySpec);
    } 
 
    private X509Certificate createCertificate() throws Exception {
        byte[] derCert = Base64.decode(TEST_CERT.getBytes(), Base64.DEFAULT);
        InputStream istream = new ByteArrayInputStream(derCert);
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        return (X509Certificate) cf.generateCertificate(istream);
    } 
 
    private String makeTemporaryContentFile(String content) throws Exception {
        FileOutputStream fw = mContext.openFileOutput("content.txt", mContext.MODE_WORLD_READABLE);
        fw.write(content.getBytes(), 0, content.length());
        fw.close();
        return mContext.getFilesDir() + "/content.txt";
    } 
 
    private String createSignature(String content, String version, String requiredHash)
                                   throws Exception { 
        Signature signer = Signature.getInstance("SHA512withRSA");
        signer.initSign(createKey());
        signer.update(content.trim().getBytes());
        signer.update(version.trim().getBytes());
        signer.update(requiredHash.getBytes());
        String sig = new String(Base64.encode(signer.sign(), Base64.DEFAULT));
        assertEquals(true,
                     verifySignature(content, version, requiredHash, sig, createCertificate()));
        return sig;
    } 
 
    public boolean verifySignature(String content, String version, String requiredPrevious,
                                   String signature, X509Certificate cert) throws Exception {
        Signature signer = Signature.getInstance("SHA512withRSA");
        signer.initVerify(cert);
        signer.update(content.trim().getBytes());
        signer.update(version.trim().getBytes());
        signer.update(requiredPrevious.trim().getBytes());
        return signer.verify(Base64.decode(signature.getBytes(), Base64.DEFAULT));
    } 
 
    private void sendIntent(String contentPath, String version, String required, String sig) {
        Intent i = new Intent();
        i.setAction("android.intent.action.UPDATE_PINS");
        i.putExtra(EXTRA_CONTENT_PATH, contentPath);
        i.putExtra(EXTRA_VERSION_NUMBER, version);
        i.putExtra(EXTRA_REQUIRED_HASH, required);
        i.putExtra(EXTRA_SIGNATURE, sig);
        mContext.sendBroadcast(i);
    } 
 
    private String runTest(String cert, String content, String version, String required, String sig)
                           throws Exception { 
        Log.e(TAG, "started test");
        overrideCert(cert);
        String contentPath = makeTemporaryContentFile(content);
        sendIntent(contentPath, version, required, sig);
        Thread.sleep(1000);
        return readPins(); 
    } 
 
    private String runTestWithoutSig(String cert, String content, String version, String required)
                                     throws Exception {