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
     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 DigestImpl<?> impl;
    16     
    17     /** Factory method is better than constructor */
    18     private Digest(DigestImpl<?> 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             DigestImpl<?> impl = DigestImpl.create(
    27                 digestor, algorithm
    28             );
    29             if (impl != null) {
    30                 return new Digest(impl);
    31             }
    32         }
    33         throw new IllegalArgumentException(algorithm);
    34     }
    35       
    36     //
    37     // these methods are kept the same as in original MessageDigest,
    38     // but for simplicity choose just some from the original API
    39     //
    40     
    41     public byte[] digest(ByteBuffer bb) {
    42         return impl.digest(bb);
    43     }
    44 }
    45 // END: day.end.bridges.Digest