/** * Fedid * * Wrapper around a federated is */ package net.deterlab.isi; import java.security.MessageDigest; import java.security.PublicKey; import java.security.NoSuchAlgorithmException; import java.security.cert.X509Certificate; public class Fedid { /** The bytes in the fedid */ protected byte[] buf; /** * Empty constructor */ public Fedid() { buf = null; } /** * Copy Constructor */ public Fedid(Fedid f) { assign( f != null ? f.getBytes() : null); } /** * Create from a byte array */ public Fedid(byte[] b) { assign(b); } /** * Create from X.509 certificate. */ public Fedid(X509Certificate c) throws NoSuchAlgorithmException { assign(c); } public void assign(byte[] b) { if ( b == null ) buf = null; else{ buf = new byte[b.length]; System.arraycopy(b, 0, buf, 0, b.length); } } public void assign(X509Certificate c) throws NoSuchAlgorithmException { if ( c != null ) { MessageDigest md = MessageDigest.getInstance("SHA1"); PublicKey pk = c.getPublicKey(); if (pk.getFormat() == "X.509" && pk.getAlgorithm() == "RSA") { // This 22 is a hack, but I don't want to parse the ASN.1 byte[] asn1 = pk.getEncoded(); byte[] bits = new byte[asn1.length -22]; System.arraycopy(asn1, 22, bits, 0, bits.length); buf = md.digest(bits); } else { throw new IllegalArgumentException("Unknown Key type"); } } else buf = null; } /** * Printable version of the Fedid * * throws Null exception if the Fedid is uninitialized */ public String toString() { String rv ="fedid:"; for ( byte b : buf) { rv += String.format("%02x", b); } return rv; } public byte[] getBytes() { return buf; } public boolean equals(Fedid f) { byte[] b = f.getBytes(); if (buf.length != b.length) return false; else { for ( int i =0; i < b.length; i++) if ( buf[i] != b[i]) return false; return true; } } }