Example usage for org.bouncycastle.bcpg ArmoredInputStream ArmoredInputStream

List of usage examples for org.bouncycastle.bcpg ArmoredInputStream ArmoredInputStream

Introduction

In this page you can find the example usage for org.bouncycastle.bcpg ArmoredInputStream ArmoredInputStream.

Prototype

public ArmoredInputStream(InputStream in, boolean hasHeaders) throws IOException 

Source Link

Document

Create an armoured input stream which will assume the data starts straight away, or parse for headers first depending on the value of hasHeaders.

Usage

From source file:org.jpos.util.PGPHelper.java

License:Open Source License

public static boolean verifySignature(InputStream in, PGPPublicKey pk)
        throws IOException, NoSuchProviderException, PGPException, SignatureException {
    boolean verify = false;
    boolean newl = false;
    int ch;//from  w w  w.j  a  v a 2s  . c o  m
    ArmoredInputStream ain = new ArmoredInputStream(in, true);
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    while ((ch = ain.read()) >= 0 && ain.isClearText()) {
        if (newl) {
            out.write((byte) '\n');
            newl = false;
        }
        if (ch == '\n') {
            newl = true;
            continue;
        }
        out.write((byte) ch);
    }
    PGPObjectFactory pgpf = new PGPObjectFactory(ain, fingerPrintCalculater);
    Object o = pgpf.nextObject();
    if (o instanceof PGPSignatureList) {
        PGPSignatureList list = (PGPSignatureList) o;
        if (list.size() > 0) {
            PGPSignature sig = list.get(0);
            sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pk);
            while ((ch = ain.read()) >= 0 && ain.isClearText()) {
                if (newl) {
                    out.write((byte) '\n');
                    newl = false;
                }
                if (ch == '\n') {
                    newl = true;
                    continue;
                }
                out.write((byte) ch);
            }
            sig.update(out.toByteArray());
            verify = sig.verify();
        }
    }
    return verify;
}

From source file:org.jpos.util.PGPHelper.java

License:Open Source License

public static int checkLicense() {
    int rc = 0x80000;
    boolean newl = false;
    int ch;/*from   w ww.j  a  va2s .c o  m*/

    try {
        InputStream in = Q2.class.getClassLoader().getResourceAsStream(Q2.LICENSEE);
        InputStream ks = Q2.class.getClassLoader().getResourceAsStream(PUBRING);
        PGPPublicKey pk = readPublicKey(ks, SIGNER);
        ArmoredInputStream ain = new ArmoredInputStream(in, true);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(pk.getFingerprint(), "HmacSHA256"));

        while ((ch = ain.read()) >= 0 && ain.isClearText()) {
            if (newl) {
                out.write((byte) '\n');
                newl = false;
            }
            if (ch == '\n') {
                newl = true;
                continue;
            }
            out.write((byte) ch);
        }
        PGPObjectFactory pgpf = new PGPObjectFactory(ain, fingerPrintCalculater);
        Object o = pgpf.nextObject();
        if (o instanceof PGPSignatureList) {
            PGPSignatureList list = (PGPSignatureList) o;
            if (list.size() > 0) {
                PGPSignature sig = list.get(0);
                sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pk);
                while ((ch = ain.read()) >= 0 && ain.isClearText()) {
                    if (newl) {
                        out.write((byte) '\n');
                        newl = false;
                    }
                    if (ch == '\n') {
                        newl = true;
                        continue;
                    }
                    out.write((byte) ch);
                }
                sig.update(out.toByteArray());
                if (sig.verify()) {
                    rc &= 0x7FFFF;
                    ByteArrayInputStream bais = new ByteArrayInputStream(out.toByteArray());
                    BufferedReader reader = new BufferedReader(new InputStreamReader(bais));
                    String s;
                    Pattern p1 = Pattern.compile("\\s(valid through:)\\s(\\d\\d\\d\\d-\\d\\d-\\d\\d)?",
                            Pattern.CASE_INSENSITIVE);
                    Pattern p2 = Pattern.compile("\\s(instances:)\\s([\\d]{0,4})?", Pattern.CASE_INSENSITIVE);
                    while ((s = reader.readLine()) != null) {
                        Matcher matcher = p1.matcher(s);
                        if (matcher.find() && matcher.groupCount() == 2) {
                            String lDate = matcher.group(2);
                            if (lDate.compareTo(Q2.getBuildTimestamp().substring(0, 10)) < 0) {
                                rc |= 0x40000;
                            }
                        }
                        matcher = p2.matcher(s);
                        if (matcher.find() && matcher.groupCount() == 2) {
                            rc |= Integer.parseInt(matcher.group(2));
                        }
                    }
                }
            }
            if (!Arrays.equals(Q2.PUBKEYHASH, mac.doFinal(pk.getEncoded())))
                rc |= 0x20000;
        }
    } catch (Exception ignored) {
        // NOPMD: signature isn't good
    }
    return rc;
}

From source file:tests.Strings.Licensee.java

public static int checkLicense() {
    int rc = 0x80000;
    System.out.println("int rc = 0x80000: " + rc);
    boolean newl = false;
    int ch;/*w  ww . ja  v a2  s  .  c  o m*/

    try {
        InputStream in = new FileInputStream(new File(LICENSEE));
        InputStream ks = new FileInputStream(new File(PUBRING));
        PGPPublicKey pk = PGPHelper.readPublicKey(ks, SIGNER);
        ArmoredInputStream ain = new ArmoredInputStream(in, true);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(ain));
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(pk.getFingerprint(), "HmacSHA256"));

        while ((ch = ain.read()) >= 0 && ain.isClearText()) {
            if (newl) {
                out.write((byte) '\n');
                newl = false;
            }
            if (ch == '\r' || ch == '\n') {
                newl = true;
                continue;
            }
            out.write((byte) ch);
        }
        String line;
        //            while ((line = br.readLine()) != null) {
        //                out.write(line.getBytes(Charset.forName("UTF-8")));
        //                out.write('\n');
        //            }

        //            ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //            if (ain != null) {
        //                br = new BufferedReader(new InputStreamReader(ain));
        //                PrintStream p = new PrintStream(baos);
        //                p.println();
        //                p.println();
        //                try {
        //                    while (br.ready() && ain.isClearText())
        //                        p.println(br.readLine());
        //                } catch (Exception ignored) {
        //                    // NOPMD ignore error
        //                }
        //            }
        PGPObjectFactory pgpf = new PGPObjectFactory(ain, new BcKeyFingerprintCalculator());
        Object o = pgpf.nextObject();
        if (o instanceof PGPSignatureList) {
            PGPSignatureList list = (PGPSignatureList) o;
            if (list.size() > 0) {
                PGPSignature sig = list.get(0);
                sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pk);
                while ((ch = ain.read()) >= 0 && ain.isClearText()) {
                    if (newl) {
                        out.write((byte) '\n');
                        newl = false;
                    }
                    if (ch == '\n') {
                        newl = true;
                        continue;
                    }
                    out.write((byte) ch);
                }
                //                    while ((line = br.readLine()) != null) {
                //                        out.write(line.getBytes());
                //                        out.write('\n');
                //                    }
                sig.update(out.toByteArray());
                if (sig.verify()) {
                    rc &= 0x7FFFF;
                    System.out.println("sig.verify(): " + rc);
                    ByteArrayInputStream bais = new ByteArrayInputStream(out.toByteArray());
                    BufferedReader reader = new BufferedReader(new InputStreamReader(bais));
                    String s;
                    Pattern p1 = Pattern.compile("\\s(valid through:)\\s(\\d\\d\\d\\d-\\d\\d-\\d\\d)?",
                            Pattern.CASE_INSENSITIVE);
                    Pattern p2 = Pattern.compile("\\s(instances:)\\s([\\d]{0,4})?", Pattern.CASE_INSENSITIVE);
                    while ((s = reader.readLine()) != null) {
                        Matcher matcher = p1.matcher(s);
                        if (matcher.find() && matcher.groupCount() == 2) {
                            String lDate = matcher.group(2);
                            if (lDate.compareTo(Q2.getBuildTimestamp().substring(0, 10)) < 0) {
                                rc |= 0x40000;
                                System.out.println("rc |= 0x40000: " + rc);
                            }
                        }
                        matcher = p2.matcher(s);
                        if (matcher.find() && matcher.groupCount() == 2) {
                            rc |= Integer.parseInt(matcher.group(2));
                            System.out.println("rc |= Integer.parseInt(matcher.group(2)): " + rc);
                        }
                    }
                }
            }
            if (!Arrays.equals(Q2.PUBKEYHASH, mac.doFinal(pk.getEncoded()))) {
                rc |= 0x20000;
                System.out.println("rc |= 0x20000: " + rc);
            }

        }
    } catch (Exception ignored) {
        // NOPMD: signature isn't good
    }
    return rc;
}