Java Digest digest(final InputStream inputStream, final MessageDigest digester)

Here you can find the source of digest(final InputStream inputStream, final MessageDigest digester)

Description

digest

License

LGPL

Declaration

public static InputStream digest(final InputStream inputStream, final MessageDigest digester) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.IOException;
import java.io.InputStream;
import java.security.*;

public class Main {
    public static InputStream digest(final InputStream inputStream, final MessageDigest digester) {
        return new DigestInputStream(removeOSSpecificChars(inputStream), digester);
    }//ww  w.ja  va  2  s. co m

    private static InputStream removeOSSpecificChars(final InputStream inputStream) {
        return new InputStream() {
            @Override
            public int read() throws IOException {
                do {
                    final byte next = (byte) inputStream.read();
                    if (!isOsSpecificChar(next)) {
                        return next;
                    }
                } while (true);
            }
        };
    }

    /**
     * EBICS Specification 14 Appendix: Signature process for the electronic signature:
     * <p></p>
     * <p>The characters restricted to the operating system (CR, LF and Ctrl-Z) are not included in the calculation
     * of hash values of the A005/A006 ES (analogous to A004 ES).</p>
     *
     * @param octet the byte
     * @return <code>true</code> if the octet reflects an OS specific character
     */
    static boolean isOsSpecificChar(final byte octet) {
        switch (octet) {
        case '\r': // CR
        case '\n': // LF
        case 0x1A: // CTRL-Z / EOF
            return true;

        default:
            return false;
        }
    }
}

Related

  1. digest(byte[] input, final String algorithm)
  2. digest(byte[] input, String algorithm, byte[] salt, int iterations)
  3. digest(byte[] message, String hash_alg)
  4. digest(final @Nullable String[] tokens, @Nullable final Date[] dates)
  5. digest(final byte[] data)
  6. digest(final java.security.MessageDigest messageDigest, final java.nio.ByteBuffer data)
  7. digest(final String algorithm, final byte[] bytes)
  8. digest(final String password)
  9. digest(InputStream input, String algorithm)