samples/messagedigest/src-new-api/org/apidesign/api/security/Digest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
parent 154 0fd5e9c500b9
permissions -rw-r--r--
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
jtulach@44
     1
jtulach@44
     2
package org.apidesign.api.security;
jtulach@44
     3
jtulach@44
     4
import java.nio.ByteBuffer;
jtulach@44
     5
import java.util.ServiceLoader;
jtulach@51
     6
import org.apidesign.spi.security.Digestor;
jtulach@44
     7
jtulach@51
     8
/** Simplified version of a Digest class that allows to compute a fingerprint
jtulach@51
     9
 * for buffer of data.
jtulach@44
    10
 *
jtulach@51
    11
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@44
    12
 */
jtulach@44
    13
// BEGIN: day.end.bridges.Digest
jtulach@44
    14
public final class Digest {
jtulach@301
    15
    private final DigestImpl<?> impl;
jtulach@44
    16
    
jtulach@44
    17
    /** Factory method is better than constructor */
jtulach@301
    18
    private Digest(DigestImpl<?> impl) {
jtulach@44
    19
        this.impl = impl;
jtulach@44
    20
    }
jtulach@44
    21
    
jtulach@44
    22
    /** Factory method to create digest for an algorithm.
jtulach@44
    23
     */
jtulach@44
    24
    public static Digest getInstance(String algorithm) {
jtulach@51
    25
        for (Digestor<?> digestor : ServiceLoader.load(Digestor.class)) {
jtulach@301
    26
            DigestImpl<?> impl = DigestImpl.create(
jtulach@154
    27
                digestor, algorithm
jtulach@154
    28
            );
jtulach@44
    29
            if (impl != null) {
jtulach@44
    30
                return new Digest(impl);
jtulach@44
    31
            }
jtulach@44
    32
        }
jtulach@44
    33
        throw new IllegalArgumentException(algorithm);
jtulach@44
    34
    }
jtulach@44
    35
      
jtulach@44
    36
    //
jtulach@44
    37
    // these methods are kept the same as in original MessageDigest,
jtulach@44
    38
    // but for simplicity choose just some from the original API
jtulach@44
    39
    //
jtulach@44
    40
    
jtulach@44
    41
    public byte[] digest(ByteBuffer bb) {
jtulach@51
    42
        return impl.digest(bb);
jtulach@44
    43
    }
jtulach@44
    44
}
jtulach@44
    45
// END: day.end.bridges.Digest