author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 09:52:23 +0200 | |
changeset 44 | 716af5f2ebd1 |
child 47 | f464a16d553a |
permissions | -rw-r--r-- |
jtulach@44 | 1 |
|
jtulach@44 | 2 |
package org.apidesign.api.security; |
jtulach@44 | 3 |
|
jtulach@44 | 4 |
import java.nio.ByteBuffer; |
jtulach@44 | 5 |
import org.apidesign.impl.security.friendapi.DigestImplementation; |
jtulach@44 | 6 |
import org.apidesign.impl.security.friendapi.DigestProvider; |
jtulach@44 | 7 |
import java.util.ServiceLoader; |
jtulach@44 | 8 |
|
jtulach@44 | 9 |
/** MessageDigest extends MessageDigestSpi, that means the javadoc |
jtulach@44 | 10 |
* |
jtulach@44 | 11 |
* @author Jaroslav Tulach |
jtulach@44 | 12 |
*/ |
jtulach@44 | 13 |
// BEGIN: day.end.bridges.Digest |
jtulach@44 | 14 |
public final class Digest { |
jtulach@44 | 15 |
private final DigestImplementation impl; |
jtulach@44 | 16 |
|
jtulach@44 | 17 |
/** Factory method is better than constructor */ |
jtulach@44 | 18 |
private Digest(DigestImplementation impl) { |
jtulach@44 | 19 |
this.impl = impl; |
jtulach@44 | 20 |
} |
jtulach@44 | 21 |
|
jtulach@44 | 22 |
/** Factory method to create digest for an algorithm. |
jtulach@44 | 23 |
*/ |
jtulach@44 | 24 |
public static Digest getInstance(String algorithm) { |
jtulach@44 | 25 |
for (DigestProvider dp : ServiceLoader.load(DigestProvider.class)) { |
jtulach@44 | 26 |
DigestImplementation impl = dp.create(algorithm); |
jtulach@44 | 27 |
if (impl != null) { |
jtulach@44 | 28 |
return new Digest(impl); |
jtulach@44 | 29 |
} |
jtulach@44 | 30 |
} |
jtulach@44 | 31 |
throw new IllegalArgumentException(algorithm); |
jtulach@44 | 32 |
} |
jtulach@44 | 33 |
|
jtulach@44 | 34 |
// |
jtulach@44 | 35 |
// these methods are kept the same as in original MessageDigest, |
jtulach@44 | 36 |
// but for simplicity choose just some from the original API |
jtulach@44 | 37 |
// |
jtulach@44 | 38 |
|
jtulach@44 | 39 |
public byte[] digest(ByteBuffer bb) { |
jtulach@44 | 40 |
impl.update(bb); |
jtulach@44 | 41 |
return impl.digest(); |
jtulach@44 | 42 |
} |
jtulach@44 | 43 |
} |
jtulach@44 | 44 |
// END: day.end.bridges.Digest |