<commit_before>/*
 *
 * Copyright 2017 Sphereon B.V.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.sphereon.libs.blockchain.commons;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;

import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * Created by niels on 3-7-2017.
 */
public class Digest {

    public static final int BUFFER_SIZE_8K = 8192;

    public enum Algorithm {
        SHA_256("SHA-256"), SHA_512("SHA-512");
        private final String algorithm;


        Algorithm(String algorithm) {
            this.algorithm = algorithm;
        }


        public String getImplementation() {
            return algorithm;
        }


        public static Algorithm from(String value) {
            if (!StringUtils.isEmpty(value) && value.contains("512")) {
                return SHA_512;
            }
            return SHA_256;
        }
    }

    public enum Encoding {
        UTF_8, HEX;
    }


    private static volatile Digest instance;


    private Digest() {
    }


    public static Digest getInstance() {
        /*
        We use double checked locking and a non final instance, since we do not know beforehand whether we operate
        within a Sring context or not.  We also provide configuration support for the Spring singleton scope
        */
        if (instance == null) {
            synchronized (Digest.class) {
                if (instance == null) {
                    Digest.instance = new Digest();
                }
            }
        }
        return instance;
    }


    public byte[] getHash(Algorithm algorithm, String input) {
        if (StringUtils.isEmpty(input)) {
            return new byte[]{};
        }
        return getHash(algorithm, input.getBytes());
    }


    public byte[] getHash(Algorithm algorithm, byte[] input) {
        try {
            return MessageDigest.getInstance(algorithm.getImplementation()).digest(input);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }


    public byte[] getHashInHexFormat(Algorithm algorithm, byte[] input) {
        byte[] hash = getHash(algorithm, input);
        return Hex.encodeHexString(hash).getBytes();
    }


    public byte[] getHashInHexFormat(Algorithm algorithm, InputStream inputStream) {
        byte[] hash = getHash(algorithm, inputStream);
        return Hex.encodeHexString(hash).getBytes();
    }


    public byte[] getHash(Algorithm algorithm, InputStream inputStream) {
        try {
            byte[] buffer = new byte[BUFFER_SIZE_8K];
            MessageDigest messageDigest = MessageDigest.getInstance(algorithm.getImplementation());
            try (DigestInputStream dis = new DigestInputStream(inputStream, messageDigest)) {
                while (dis.read(buffer) != -1)
                    ;
            }
            return messageDigest.digest();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }


    public byte[] getSHA256Hash(byte[] base) {
        return getHash(Algorithm.SHA_256, base);
    }


    public byte[] getSHA512Hash(byte[] base) {
        return getHash(Algorithm.SHA_512, base);
    }


    public String getHashAsString(Algorithm algorithm, String input, Encoding encoding) {
        if (StringUtils.isEmpty(input)) {
            return "";
        }
        return getHashAsString(algorithm, input.getBytes(), encoding);

    }


    public String getHashAsString(Algorithm algorithm, byte[] input, Encoding encoding) {
        byte[] hash = getHash(algorithm, input);
        if (encoding == Encoding.UTF_8) {
            return new String(hash, Charset.forName("UTF-8"));
        } else {
            return Hex.encodeHexString(hash);
        }
    }

}<commit_msg>SPMS-166  - Rename hash methos, make uniform<commit_after>/*
 *
 * Copyright 2017 Sphereon B.V.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.sphereon.libs.blockchain.commons;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;

import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * Created by niels on 3-7-2017.
 */
public class Digest {

    public static final int BUFFER_SIZE_8K = 8192;

    public enum Algorithm {
        SHA_256("SHA-256"), SHA_512("SHA-512");
        private final String algorithm;


        Algorithm(String algorithm) {
            this.algorithm = algorithm;
        }


        public String getImplementation() {
            return algorithm;
        }


        public static Algorithm from(String value) {
            if (!StringUtils.isEmpty(value) && value.contains("512")) {
                return SHA_512;
            }
            return SHA_256;
        }
    }

    public enum Encoding {
        UTF_8, HEX;
    }


    private static volatile Digest instance;


    private Digest() {
    }


    public static Digest getInstance() {
        /*
        We use double checked locking and a non final instance, since we do not know beforehand whether we operate
        within a Sring context or not.  We also provide configuration support for the Spring singleton scope
        */
        if (instance == null) {
            synchronized (Digest.class) {
                if (instance == null) {
                    Digest.instance = new Digest();
                }
            }
        }
        return instance;
    }


    public byte[] getHash(Algorithm algorithm, String input) {
        if (StringUtils.isEmpty(input)) {
            return new byte[]{};
        }
        return getHash(algorithm, input.getBytes());
    }


    public byte[] getHash(Algorithm algorithm, byte[] input) {
        try {
            return MessageDigest.getInstance(algorithm.getImplementation()).digest(input);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }


    public byte[] getHash(Algorithm algorithm, InputStream inputStream) {
        try {
            byte[] buffer = new byte[BUFFER_SIZE_8K];
            MessageDigest messageDigest = MessageDigest.getInstance(algorithm.getImplementation());
            try (DigestInputStream dis = new DigestInputStream(inputStream, messageDigest)) {
                while (dis.read(buffer) != -1)
                    ;
            }
            return messageDigest.digest();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }



    public byte[] getHashAsHex(Algorithm algorithm, byte[] input) {
        return getHashAsString(algorithm, input, Encoding.HEX).getBytes();
    }


    public byte[] getHashAsHex(Algorithm algorithm, InputStream input) {
        return getHashAsString(algorithm, input, Encoding.HEX).getBytes();
    }


    public byte[] getSHA256Hash(byte[] base) {
        return getHash(Algorithm.SHA_256, base);
    }


    public byte[] getSHA512Hash(byte[] base) {
        return getHash(Algorithm.SHA_512, base);
    }


    public String getHashAsString(Algorithm algorithm, String input, Encoding encoding) {
        if (StringUtils.isEmpty(input)) {
            return "";
        }
        return getHashAsString(algorithm, input.getBytes(), encoding);

    }


    public String getHashAsString(Algorithm algorithm, byte[] input, Encoding encoding) {
        byte[] hash = getHash(algorithm, input);
        if (encoding == Encoding.UTF_8) {
            return new String(hash, Charset.forName("UTF-8"));
        } else {
            return Hex.encodeHexString(hash);
        }
    }

    public String getHashAsString(Algorithm algorithm, InputStream input, Encoding encoding) {
        byte[] hash = getHash(algorithm, input);
        if (encoding == Encoding.UTF_8) {
            return new String(hash, Charset.forName("UTF-8"));
        } else {
            return Hex.encodeHexString(hash);
        }
    }

}
{'fraction_non_alphanumeric': 0.0957274202271498, 'fraction_numerical': 0.0130881557598702, 'mean_word_length': 2.3524292965917333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5216277', 'n_tokens_mistral': 2553, 'n_tokens_neox': 2381, 'n_words': 711}