jtulach@44: jtulach@44: package org.apidesign.api.security; jtulach@44: jtulach@44: import java.nio.ByteBuffer; jtulach@44: import java.util.ServiceLoader; jtulach@51: import org.apidesign.spi.security.Digestor; jtulach@44: jtulach@51: /** Simplified version of a Digest class that allows to compute a fingerprint jtulach@51: * for buffer of data. jtulach@44: * jtulach@51: * @author Jaroslav Tulach jtulach@44: */ jtulach@44: // BEGIN: day.end.bridges.Digest jtulach@44: public final class Digest { jtulach@301: private final DigestImpl impl; jtulach@44: jtulach@44: /** Factory method is better than constructor */ jtulach@301: private Digest(DigestImpl impl) { jtulach@44: this.impl = impl; jtulach@44: } jtulach@44: jtulach@44: /** Factory method to create digest for an algorithm. jtulach@44: */ jtulach@44: public static Digest getInstance(String algorithm) { jtulach@51: for (Digestor digestor : ServiceLoader.load(Digestor.class)) { jtulach@301: DigestImpl impl = DigestImpl.create( jtulach@154: digestor, algorithm jtulach@154: ); jtulach@44: if (impl != null) { jtulach@44: return new Digest(impl); jtulach@44: } jtulach@44: } jtulach@44: throw new IllegalArgumentException(algorithm); jtulach@44: } jtulach@44: jtulach@44: // jtulach@44: // these methods are kept the same as in original MessageDigest, jtulach@44: // but for simplicity choose just some from the original API jtulach@44: // jtulach@44: jtulach@44: public byte[] digest(ByteBuffer bb) { jtulach@51: return impl.digest(bb); jtulach@44: } jtulach@44: } jtulach@44: // END: day.end.bridges.Digest