Package gnu.javax.crypto.keyring

Examples of gnu.javax.crypto.keyring.GnuPrivateKeyring


  public void test(TestHarness harness)
  {
    harness.checkPoint("TestOfGnuPrivateKeyring");
    try
      {
        GnuPrivateKeyring kr1 = new GnuPrivateKeyring();
        // store a private key
        PrivateKey pk1 = new DSSPrivateKey(Registry.ASN1_ENCODING_ID, p, q, g, x);
        kr1.putPrivateKey(ALIAS, pk1, KEY_PASSWORD);
        // retrieve the same private key
        PrivateKey pk2 = (PrivateKey) kr1.getPrivateKey(ALIAS, KEY_PASSWORD);
        // check that private key is the same
        harness.check(pk2, pk1, "In-memory and original private key MUST be equal");
        // store a public key
        PublicKey k1 = new DSSPublicKey(Registry.ASN1_ENCODING_ID, p, q, g, y);
        kr1.putPublicKey(ALIAS, k1);
        // retrieve the same public key
        PublicKey k2 = kr1.getPublicKey(ALIAS);
        // check that public key is the same
        harness.check(k2, k1, "In-memory and original public key MUST be equal");
        // store a certificate
        CertificateFactory x509Factory = CertificateFactory.getInstance("X.509");
        byte[] certificateBytes = SELF_SIGNED_CERT.getBytes("ASCII");
        ByteArrayInputStream bais = new ByteArrayInputStream(certificateBytes);
        Certificate certificate = x509Factory.generateCertificate(bais);
        Certificate[] chain1 = new Certificate[] { certificate };
        kr1.putCertPath(ALIAS, chain1);
        // retrieve the same certificate path
        Certificate[] chain2 = kr1.getCertPath(ALIAS);
        harness.check(Arrays.equals(chain2, chain1),
                      "In-memory and original certificate MUST be equal");

        // persist the keyring
        File ksFile = File.createTempFile("gkr-", ".gks");
        ksFile.deleteOnExit();
        FileOutputStream fos = new FileOutputStream(ksFile);
        HashMap attributes = new HashMap();
        attributes.put(IKeyring.KEYRING_DATA_OUT, fos);
        attributes.put(IKeyring.KEYRING_PASSWORD, STORE_PASSWORD);
        kr1.store(attributes);
        fos.flush();
        fos.close();

        // re-load it from the persisted file
        GnuPrivateKeyring kr2 = new GnuPrivateKeyring();
        FileInputStream fis = new FileInputStream(ksFile);
        attributes.clear();
        attributes.put(IKeyring.KEYRING_DATA_IN, fis);
        attributes.put(IKeyring.KEYRING_PASSWORD, STORE_PASSWORD);
        kr2.load(attributes);
        fis.close();

        // retrieve the same private key
        PrivateKey pk3 = (PrivateKey) kr2.getPrivateKey(ALIAS, KEY_PASSWORD);
        // check that it is still the same
        harness.check(pk3, pk1, "Persisted and original private key MUST be equal");
        // retrieve the same public key
        PublicKey k3 = kr2.getPublicKey(ALIAS);
        // check that it is still the same
        harness.check(k3, k1, "Persisted and original public key MUST be equal");
        // retrieve the certificate chain
        Certificate[] chain3 = kr2.getCertPath(ALIAS);
        // check that it's still the same certificate
        harness.check(Arrays.equals(chain3, chain1),
                      "Persisted and original certificate MUST be equal");
      }
    catch (Exception x)
View Full Code Here


  private static final String ALIAS = "mykey";

  public void test(final TestHarness harness)
  {
    harness.checkPoint("TestOfPrivateKeyring");
    final GnuPrivateKeyring kr = new GnuPrivateKeyring();
    try
      {
        final Map attributes = new HashMap();
        attributes.put(IKeyring.KEYRING_DATA_IN,
                       new ByteArrayInputStream(keyring));
        attributes.put(IKeyring.KEYRING_PASSWORD, "password".toCharArray());

        Security.addProvider(new Gnu());

        kr.load(attributes);
        harness.check(true, "load(...)");

        harness.check(kr.containsPrivateKey(ALIAS), "containsCertificate(...)");

        final List list = kr.get(ALIAS);
//        System.out.println("list.size()="+list.size());
        harness.check(list.size() == 2, "get(...).size() == 2");

//         for (java.util.Iterator it = list.listIterator(); it.hasNext(); ) {
//           System.out.println("*** "+it.next());
 
View Full Code Here

TOP

Related Classes of gnu.javax.crypto.keyring.GnuPrivateKeyring

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.