samples/messagedigest/src-new-api/org/apidesign/api/security/Digest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:26 +0200
changeset 50 019f1e9f7741
parent 47 f464a16d553a
child 51 130e099942d8
permissions -rw-r--r--
up to line 4500
jtulach@44
     1
jtulach@44
     2
package org.apidesign.api.security;
jtulach@44
     3
jtulach@44
     4
import java.nio.ByteBuffer;
jtulach@50
     5
import org.apidesign.impl.security.friendapi.DigestImplementation;
jtulach@50
     6
import org.apidesign.impl.security.friendapi.DigestProvider;
jtulach@44
     7
import java.util.ServiceLoader;
jtulach@44
     8
jtulach@50
     9
/** MessageDigest extends MessageDigestSpi, that means the javadoc
jtulach@44
    10
 *
jtulach@50
    11
 * @author Jaroslav Tulach
jtulach@44
    12
 */
jtulach@44
    13
// BEGIN: day.end.bridges.Digest
jtulach@44
    14
public final class Digest {
jtulach@50
    15
    private final DigestImplementation impl;
jtulach@44
    16
    
jtulach@44
    17
    /** Factory method is better than constructor */
jtulach@50
    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@50
    25
        for (DigestProvider dp : ServiceLoader.load(DigestProvider.class)) {
jtulach@50
    26
            DigestImplementation impl = dp.create(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@50
    40
        impl.update(bb);
jtulach@50
    41
        return impl.digest();
jtulach@44
    42
    }
jtulach@44
    43
}
jtulach@44
    44
// END: day.end.bridges.Digest