samples/messagedigest/src-new-api/org/apidesign/api/security/Digest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:25 +0200
changeset 47 f464a16d553a
parent 44 716af5f2ebd1
child 50 019f1e9f7741
permissions -rw-r--r--
Simplified to does not contain the friend API, instead the SPI is directly define by the API
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@47
     6
import org.apidesign.spi.security.Digestor;
jtulach@44
     7
jtulach@47
     8
/** Simplified version of a Digest class that allows to compute a fingerprint
jtulach@47
     9
 * for buffer of data.
jtulach@44
    10
 *
jtulach@47
    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@47
    15
    private final DigestImplementation<?> impl;
jtulach@44
    16
    
jtulach@44
    17
    /** Factory method is better than constructor */
jtulach@47
    18
    private Digest(DigestImplementation<?> 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@47
    25
        for (Digestor<?> digestor : ServiceLoader.load(Digestor.class)) {
jtulach@47
    26
            DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);
jtulach@44
    27
            if (impl != null) {
jtulach@44
    28
                return new Digest(impl);
jtulach@44
    29
            }
jtulach@44
    30
        }
jtulach@44
    31
        throw new IllegalArgumentException(algorithm);
jtulach@44
    32
    }
jtulach@44
    33
      
jtulach@44
    34
    //
jtulach@44
    35
    // these methods are kept the same as in original MessageDigest,
jtulach@44
    36
    // but for simplicity choose just some from the original API
jtulach@44
    37
    //
jtulach@44
    38
    
jtulach@44
    39
    public byte[] digest(ByteBuffer bb) {
jtulach@47
    40
        return impl.digest(bb);
jtulach@44
    41
    }
jtulach@44
    42
}
jtulach@44
    43
// END: day.end.bridges.Digest