Example usage for org.bouncycastle.openpgp PGPPublicKey getEncoded

List of usage examples for org.bouncycastle.openpgp PGPPublicKey getEncoded

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPPublicKey getEncoded.

Prototype

public byte[] getEncoded() throws IOException 

Source Link

Usage

From source file:google.registry.keyring.api.ComparatorKeyring.java

License:Open Source License

@VisibleForTesting
static boolean compare(@Nullable PGPPublicKey a, @Nullable PGPPublicKey b) {
    if (a == null || b == null) {
        return a == null && b == null;
    }/*  w w  w. j a v  a 2s.c  o  m*/
    try {
        return Arrays.equals(a.getFingerprint(), b.getFingerprint())
                && Arrays.equals(a.getEncoded(), b.getEncoded());
    } catch (IOException e) {
        logger.severefmt("ComparatorKeyring error: PGPPublicKey.getEncoded failed: %s", e);
        return false;
    }
}

From source file:google.registry.keyring.api.PgpHelper.java

License:Open Source License

/** Converts {@code publicKey} to bytes. */
public static byte[] convertPublicKeyToBytes(PGPPublicKey publicKey) {
    try {/*from  www .j a v a  2s . c  o m*/
        return publicKey.getEncoded();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

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  www  . j a  v a2s  .co 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;//from   ww w. j a v a2s .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;
}