jtulach@44: /* jtulach@44: * To change this template, choose Tools | Templates jtulach@44: * and open the template in the editor. jtulach@44: */ jtulach@44: jtulach@44: package org.apidesign.impl.security.extension; jtulach@44: jtulach@44: import org.apidesign.api.security.Digest; jtulach@44: import java.nio.ByteBuffer; jtulach@44: import java.security.MessageDigest; jtulach@44: import java.security.NoSuchAlgorithmException; jtulach@44: import java.security.Provider; jtulach@44: import java.security.Security; jtulach@44: import java.util.Collections; jtulach@44: import java.util.List; jtulach@44: import java.util.Map; jtulach@44: jtulach@44: /** jtulach@44: * jtulach@44: * @author Jaroslav Tulach jtulach@44: */ jtulach@50: public class BridgeToOld extends Provider { jtulach@44: jtulach@44: public BridgeToOld() { jtulach@44: super("spi.Digestor", 1.0, ""); jtulach@44: Security.addProvider(this); jtulach@44: } jtulach@44: jtulach@44: @Override jtulach@44: public synchronized Service getService(String type, String algorithm) { jtulach@50: if ("MessageDigest".equals(type)) { jtulach@50: Digest dig = Digest.getInstance(algorithm); jtulach@50: if (dig != null) { jtulach@50: return new ServiceImpl(dig, this, type, algorithm, "", Collections.emptyList(), Collections.emptyMap()); jtulach@44: } jtulach@44: } jtulach@50: return null; jtulach@44: } jtulach@44: jtulach@44: private static class ServiceImpl extends Service { jtulach@44: Digest dig; jtulach@44: jtulach@44: public ServiceImpl(Digest dig, Provider provider, String type, String algorithm, String className, List aliases, Map attributes) { jtulach@44: super(provider, type, algorithm, className, aliases, attributes); jtulach@44: this.dig = dig; jtulach@44: } jtulach@44: jtulach@44: @Override jtulach@44: public Object newInstance(Object constructorParameter) throws NoSuchAlgorithmException { jtulach@44: return new MessageDigest(getAlgorithm()) { jtulach@44: private byte[] res; jtulach@44: jtulach@44: @Override jtulach@44: protected void engineUpdate(byte input) { jtulach@44: ByteBuffer bb = ByteBuffer.wrap(new byte[] { input }); jtulach@44: res = dig.digest(bb); jtulach@44: } jtulach@44: jtulach@44: @Override jtulach@44: protected void engineUpdate(byte[] input, int offset, int len) { jtulach@44: ByteBuffer bb = ByteBuffer.wrap(input); jtulach@44: bb.position(offset); jtulach@44: bb.limit(offset + len); jtulach@44: res = dig.digest(bb); jtulach@44: } jtulach@44: jtulach@44: @Override jtulach@44: protected byte[] engineDigest() { jtulach@44: return res; jtulach@44: } jtulach@44: jtulach@44: @Override jtulach@44: protected void engineReset() { jtulach@44: dig = Digest.getInstance(getAlgorithm()); jtulach@44: } jtulach@44: }; jtulach@44: } jtulach@44: jtulach@44: jtulach@44: } jtulach@44: jtulach@44: }