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
     1 
     2 package org.apidesign.api.security;
     3 
     4 import java.nio.ByteBuffer;
     5 import java.util.ServiceLoader;
     6 import org.apidesign.spi.security.Digestor;
     7 
     8 /** Simplified version of a Digest class that allows to compute a fingerprint
     9  * for buffer of data.
    10  *
    11  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    12  */
    13 // BEGIN: day.end.bridges.Digest
    14 public final class Digest {
    15     private final DigestImplementation<?> impl;
    16     
    17     /** Factory method is better than constructor */
    18     private Digest(DigestImplementation<?> impl) {
    19         this.impl = impl;
    20     }
    21     
    22     /** Factory method to create digest for an algorithm.
    23      */
    24     public static Digest getInstance(String algorithm) {
    25         for (Digestor<?> digestor : ServiceLoader.load(Digestor.class)) {
    26             DigestImplementation<?> impl = DigestImplementation.create(digestor, algorithm);
    27             if (impl != null) {
    28                 return new Digest(impl);
    29             }
    30         }
    31         throw new IllegalArgumentException(algorithm);
    32     }
    33       
    34     //
    35     // these methods are kept the same as in original MessageDigest,
    36     // but for simplicity choose just some from the original API
    37     //
    38     
    39     public byte[] digest(ByteBuffer bb) {
    40         return impl.digest(bb);
    41     }
    42 }
    43 // END: day.end.bridges.Digest