source: axis/net/deterlab/fedd/Fedid.java @ 55de6a9

axis_examplecompt_changesinfo-ops
Last change on this file since 55de6a9 was 55de6a9, checked in by Ted Faber <faber@…>, 13 years ago

Example of using java to talk to fedd

  • Property mode set to 100644
File size: 2.0 KB
Line 
1/**
2 * Fedid
3 *
4 * Wrapper around a federated is
5 */
6
7package net.deterlab.isi;
8
9import java.security.MessageDigest;
10import java.security.PublicKey;
11import java.security.NoSuchAlgorithmException;
12import java.security.cert.X509Certificate;
13
14public class Fedid {
15
16    /** The bytes in the fedid */
17    protected byte[] buf;
18
19
20    /**
21     * Empty constructor
22     */
23    public Fedid() { buf = null; }
24
25    /**
26     * Copy Constructor
27     */
28    public Fedid(Fedid f) { assign( f != null ? f.getBytes() : null); }
29    /**
30     * Create from a byte array
31     */
32    public Fedid(byte[] b) { assign(b); }
33
34    /**
35     * Create from X.509 certificate.
36     */
37    public Fedid(X509Certificate c) throws NoSuchAlgorithmException {
38        assign(c);
39    }
40
41    public void assign(byte[] b) { 
42        if ( b == null )
43            buf = null;
44        else{
45            buf = new byte[b.length];
46            System.arraycopy(b, 0, buf, 0, b.length);
47        }
48    }
49
50    public void assign(X509Certificate c) throws NoSuchAlgorithmException {
51        if ( c != null ) {
52            MessageDigest md = MessageDigest.getInstance("SHA1");
53            PublicKey pk = c.getPublicKey();
54           
55            if (pk.getFormat() == "X.509" && pk.getAlgorithm() == "RSA") {
56                // This 22 is a hack, but I don't want to parse the ASN.1
57                byte[] asn1 = pk.getEncoded();
58                byte[] bits = new byte[asn1.length -22];
59
60                System.arraycopy(asn1, 22, bits, 0, bits.length);
61
62                buf = md.digest(bits);
63            }
64            else {
65                throw new IllegalArgumentException("Unknown Key type");
66            }
67        }
68        else buf = null;
69    }
70
71    /**
72     * Printable version of the Fedid
73     *
74     * throws Null exception if the Fedid is uninitialized
75     */
76    public String toString() {
77        String rv ="fedid:";
78
79        for ( byte b : buf) {
80            rv += String.format("%02x", b);
81        }
82        return rv;
83    }
84
85    public byte[] getBytes() { return buf; }
86
87    public boolean equals(Fedid f) {
88        byte[] b = f.getBytes();
89
90        if (buf.length != b.length) return false;
91        else {
92            for ( int i =0; i < b.length; i++) 
93                if ( buf[i] != b[i]) return false;
94            return true;
95        }
96    }
97}
Note: See TracBrowser for help on using the repository browser.