package com.palmnest.webapp.controller.utils;

import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.util.Arrays;

public class SignTools {

	private static String publicKey  = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCyzKhXkP76cx1PWwUz3n4G3JjFAeJGlZhiV/GEn1ZMGgWioy42GTjFBNq71jnQd8ycErP8uZUPaoleuCYGWSG0Rr9NIi3GNUsQQHpDkdH7IPwxMUH+1jKDGcV6fRW5knUTKw6vqYfBagbIUSNeST3efRdVWn0wLaF/Or0lYhmZBwIDAQAB";//RSAUtils.getPublicKey(keyMap);
	private static String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALLMqFeQ/vpzHU9bBTPefgbcmMUB4kaVmGJX8YSfVkwaBaKjLjYZOMUE2rvWOdB3zJwSs/y5lQ9qiV64JgZZIbRGv00iLcY1SxBAekOR0fsg/DExQf7WMoMZxXp9FbmSdRMrDq+ph8FqBshRI15JPd59F1VafTAtoX86vSViGZkHAgMBAAECgYAmcM24ldnb6qfgiccikbEytHa71F3bownyCspIIyGglu+jOcYTp3SHFzYXsEUZxsfcvkCoExYzDEqgbKLwNnWCOcLUbSWpuWDykTomJ5Q2jvGX+Sy7/IkGAquLkvJS0Agqdl9Q35hZZZi5W64MeCeyn/Svl7evJpfb9QKcCovsoQJBAPd6vSiMf8hs3sC6xRb8tnDw5H1K8slyXbB2ZlNpb8Blci9y0ACN708PcLItf5e6+XwdkAt1mjgkjv6QpIRY/f8CQQC49JRWCROR2P9+t7FSRfxauFwrP8+GNacbTLZdrJ6ptVICzsvyysgTu5Xlig2HdySsigD31Pzc1SgbWbmRjXT5AkEA7L9fS/Y0nCECa6+Asj/prFvWycG0R2CAHjr2It+fx5eEx713HMCraZ+kuN9ry1RULRb+4OeNNB5NKVxn9jBIMwJAEEioTrLZ4DtkszllCUNUJoCYzM3/bWeH4OVuIO0zwulN4SVn22ZzGxmm5uCytDyPJAm6cBd2c9Lohw/tQJ1dsQJBAKs6h3jhGSR6IwZp/tWMe96rQGh+d/Br6yzH11rQ6amht7d56dMSDnUSBiXJgVoA9lgYkFgvbgnKJwtfrYv+CwE=";//RSAUtils.getPrivateKey(keyMap);

	public static Boolean checkSign(String content, String sign) throws Exception {

//		return true;

		byte[] encodedData = RSAUtils.encryptByPrivateKey(content.getBytes(), privateKey);

		boolean status = RSAUtils.verify(encodedData, publicKey, sign);

		return status;
	}

	public static String makeSign(String param) throws Exception {
		byte[] encodedData = RSAUtils.encryptByPrivateKey(param.getBytes(), privateKey);
		String sign        = RSAUtils.sign(encodedData, privateKey);
		return sign;
	}

	public static String getFoodSign(String str) throws Exception {
		MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
		messageDigest.update(str.getBytes("UTF-8"));
		byte byteBuffer[] = messageDigest.digest();
		return Base64.encodeBase64URLSafeString(byteBuffer);
	}

	public static String ASCIISort(String str) {
		char[]        test = new char[str.length()];
		StringBuilder sb   = new StringBuilder();
		while (true) {
			String a = str;//直接读取这行当中的字符串。
			for (int i = 0; i < str.length(); i++) {
				test[i] = a.charAt(i);//字符串处理每次读取一位。
			}
			Arrays.sort(test);
			for (int i = 0; i < test.length; i++) {
				sb.append(test[i]);
			}
			String trim = sb.toString().trim();
			return trim;
		}
	}

	public static String readInputStream(InputStream is, Charset charset) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		byte[]                buffer       = new byte[1024];
		int                   len          = 0;
		String                result       = "";
		if (is != null) {
			try {
				while ((len = is.read(buffer)) != -1) {
					outputStream.write(buffer, 0, len);
				}
				result = new String(outputStream.toByteArray(), charset);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}


}
