samples/messagedigest/src-new-api/org/apidesign/api/security/DigestImplementation.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:25 +0200
changeset 47 f464a16d553a
permissions -rw-r--r--
Simplified to does not contain the friend API, instead the SPI is directly define by the API
jtulach@47
     1
/*
jtulach@47
     2
 * To change this template, choose Tools | Templates
jtulach@47
     3
 * and open the template in the editor.
jtulach@47
     4
 */
jtulach@47
     5
jtulach@47
     6
package org.apidesign.api.security;
jtulach@47
     7
jtulach@47
     8
import java.nio.ByteBuffer;
jtulach@47
     9
import org.apidesign.spi.security.Digestor;
jtulach@47
    10
jtulach@47
    11
/**
jtulach@47
    12
 *
jtulach@47
    13
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@47
    14
 */
jtulach@47
    15
final class DigestImplementation<Data> {
jtulach@47
    16
    private static final DigestorAccessorImpl ACCESSOR = new DigestorAccessorImpl();
jtulach@47
    17
    
jtulach@47
    18
    private final Digestor<Data> digestor;
jtulach@47
    19
    private final String algorithm;
jtulach@47
    20
    private Data data;
jtulach@47
    21
    
jtulach@47
    22
    private DigestImplementation(Digestor<Data> digestor, String algorithm, Data d) {
jtulach@47
    23
        this.digestor = digestor;
jtulach@47
    24
        this.algorithm = algorithm;
jtulach@47
    25
        this.data = d;
jtulach@47
    26
    }
jtulach@47
    27
    
jtulach@47
    28
    static <Data> DigestImplementation create(Digestor<Data> digestor, String algorithm) {
jtulach@47
    29
        Data d = ACCESSOR.create(digestor, algorithm);
jtulach@47
    30
        if (d == null) {
jtulach@47
    31
            return null;
jtulach@47
    32
        } else {
jtulach@47
    33
            return new DigestImplementation(digestor, algorithm, d);
jtulach@47
    34
        }
jtulach@47
    35
    }
jtulach@47
    36
jtulach@47
    37
    byte[] digest(ByteBuffer bb) {
jtulach@47
    38
        ACCESSOR.update(digestor, data, bb);
jtulach@47
    39
        return ACCESSOR.digest(digestor, data);
jtulach@47
    40
    }
jtulach@47
    41
}