<commit_before><commit_msg>Utility to generate SSH RSA public key fingerprint<commit_after>package org.xdi.oxauth.model.util;

import java.io.ByteArrayOutputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;

import org.apache.commons.codec.binary.Hex;
import org.apache.log4j.Logger;

/**
 * @author <NAME>
 * @version March 03, 2016
 */
public class FingerprintUtil {

	private static final Logger log = Logger.getLogger(FingerprintUtil.class);

	/*
	 * Return SSH RSA public key fingerprint
	 */
	public static Object getPublicKeyFingerprint(PublicKey publicKey) throws NoSuchAlgorithmException, IOException {
		if (publicKey instanceof RSAPublicKey) {
			return getPublicKeyFingerprint((RSAPublicKey) publicKey);
		}
		
		throw new NoSuchAlgorithmException("Unsopported PublicKey type");
	}

	public static String getPublicKeyFingerprint(RSAPublicKey publicKey) throws NoSuchAlgorithmException, IOException {
		MessageDigest digest = MessageDigest.getInstance("MD5");
		
		byte[] derEncoded = getDerEncoding(publicKey);
		byte[] fingerprint = digest.digest(derEncoded);

		return Hex.encodeHexString(fingerprint);
	}

	private static byte[] getDerEncoding(RSAPublicKey key) throws IOException {
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		DataOutputStream dataOutput = new DataOutputStream(buffer);
		writeVariableLengthOpaque("ssh-rsa".getBytes(), dataOutput);
		writeVariableLengthOpaque(key.getPublicExponent().toByteArray(), dataOutput);
		writeVariableLengthOpaque(key.getModulus().toByteArray(), dataOutput);

		return buffer.toByteArray();
	}

	private static void writeVariableLengthOpaque(byte[] opaque, DataOutput byteBuffer) throws IOException {
		byteBuffer.writeInt(opaque.length);
		byteBuffer.write(opaque);
	}

}
{'fraction_non_alphanumeric': 0.09346271705822268, 'fraction_numerical': 0.0040858018386108275, 'mean_word_length': 6.448669201520913, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1372813', 'n_tokens_mistral': 597, 'n_tokens_neox': 545, 'n_words': 123}