1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/messagedigest/src-new-api/org/apidesign/api/security/Digest.java Sat Jun 14 09:52:23 2008 +0200
1.3 @@ -0,0 +1,44 @@
1.4 +
1.5 +package org.apidesign.api.security;
1.6 +
1.7 +import java.nio.ByteBuffer;
1.8 +import org.apidesign.impl.security.friendapi.DigestImplementation;
1.9 +import org.apidesign.impl.security.friendapi.DigestProvider;
1.10 +import java.util.ServiceLoader;
1.11 +
1.12 +/** MessageDigest extends MessageDigestSpi, that means the javadoc
1.13 + *
1.14 + * @author Jaroslav Tulach
1.15 + */
1.16 +// BEGIN: day.end.bridges.Digest
1.17 +public final class Digest {
1.18 + private final DigestImplementation impl;
1.19 +
1.20 + /** Factory method is better than constructor */
1.21 + private Digest(DigestImplementation impl) {
1.22 + this.impl = impl;
1.23 + }
1.24 +
1.25 + /** Factory method to create digest for an algorithm.
1.26 + */
1.27 + public static Digest getInstance(String algorithm) {
1.28 + for (DigestProvider dp : ServiceLoader.load(DigestProvider.class)) {
1.29 + DigestImplementation impl = dp.create(algorithm);
1.30 + if (impl != null) {
1.31 + return new Digest(impl);
1.32 + }
1.33 + }
1.34 + throw new IllegalArgumentException(algorithm);
1.35 + }
1.36 +
1.37 + //
1.38 + // these methods are kept the same as in original MessageDigest,
1.39 + // but for simplicity choose just some from the original API
1.40 + //
1.41 +
1.42 + public byte[] digest(ByteBuffer bb) {
1.43 + impl.update(bb);
1.44 + return impl.digest();
1.45 + }
1.46 +}
1.47 +// END: day.end.bridges.Digest