samples/messagedigest/src-new-api/org/apidesign/api/security/DigestImplementation.java
changeset 47 f464a16d553a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/messagedigest/src-new-api/org/apidesign/api/security/DigestImplementation.java	Sat Jun 14 09:52:25 2008 +0200
     1.3 @@ -0,0 +1,41 @@
     1.4 +/*
     1.5 + * To change this template, choose Tools | Templates
     1.6 + * and open the template in the editor.
     1.7 + */
     1.8 +
     1.9 +package org.apidesign.api.security;
    1.10 +
    1.11 +import java.nio.ByteBuffer;
    1.12 +import org.apidesign.spi.security.Digestor;
    1.13 +
    1.14 +/**
    1.15 + *
    1.16 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    1.17 + */
    1.18 +final class DigestImplementation<Data> {
    1.19 +    private static final DigestorAccessorImpl ACCESSOR = new DigestorAccessorImpl();
    1.20 +    
    1.21 +    private final Digestor<Data> digestor;
    1.22 +    private final String algorithm;
    1.23 +    private Data data;
    1.24 +    
    1.25 +    private DigestImplementation(Digestor<Data> digestor, String algorithm, Data d) {
    1.26 +        this.digestor = digestor;
    1.27 +        this.algorithm = algorithm;
    1.28 +        this.data = d;
    1.29 +    }
    1.30 +    
    1.31 +    static <Data> DigestImplementation create(Digestor<Data> digestor, String algorithm) {
    1.32 +        Data d = ACCESSOR.create(digestor, algorithm);
    1.33 +        if (d == null) {
    1.34 +            return null;
    1.35 +        } else {
    1.36 +            return new DigestImplementation(digestor, algorithm, d);
    1.37 +        }
    1.38 +    }
    1.39 +
    1.40 +    byte[] digest(ByteBuffer bb) {
    1.41 +        ACCESSOR.update(digestor, data, bb);
    1.42 +        return ACCESSOR.digest(digestor, data);
    1.43 +    }
    1.44 +}