package cn.superdesk.uniorder.common.security.crypt.rsa;

import cn.superdesk.uniorder.common.Node;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class UnifiedSignUtils {

	private static String publicKey  = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCPaaREeRB2lw010c/UWTN7dfWuWt2nV5YB2W/uJzq03vbq4xsRjQ5lkVV7t1P+v3pC60zmhiG00y+JqI5uq239g/cizgDMZpk5EONiJeyIVkS0isXkGwuGWxVqZveX/kDQoCRoPXaXAxBPGVnmLyC0YSsIL+9ej4qUFpp7fmKZKwIDAQAB";
	private static String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAI9ppER5EHaXDTXRz9RZM3t19a5a3adXlgHZb+4nOrTe9urjGxGNDmWRVXu3U/6/ekLrTOaGIbTTL4mojm6rbf2D9yLOAMxmmTkQ42Il7IhWRLSKxeQbC4ZbFWpm95f+QNCgJGg9dpcDEE8ZWeYvILRhKwgv716PipQWmnt+YpkrAgMBAAECgYBxzZ0jEQb1dGxxNuu7UIXABR1OnKCj+Kb7FLbzv4GgogZC4rFKPiscgjXZlOM8lmnIzLwbenko6yUgEffk7ni6si9iEdmfWtrATCbPB3bSIRv2/mNMNYgIm+NAwJ05m1GUv/8yvSK0YmJHJNkZWiluWPgbPRYqbtFR/pd6CrbC0QJBAMe1IJYvEg7WyCHTHF5ojq+i4Rbl4shf75i5bEax1jahcsCJgRo01902aGcGWQ5M4KeQK/8xiT38cCy9qvByliUCQQC31kiBYfsj71GkVZ11LrCXZD6xU4EEwW8uwPC6RzkHoQUzktEGL+33JKWYhXlxHBZD+XM1BGecVtY6F5siaokPAkEAgJZ5svn/8JoPJuhdvdQo+6IuuKWwzkAnb/kEZMihG2/9CUgAXrVjwMYFl0CZGwIZLQtBqQFmUwk12TkO1d+SGQJAL/HOzw+CtAiCPl2lxx36KEGYqB6jE15vZL4lCi/xC7BO7vhnUznE0iPYnEKTPZCZrfistzMC4p8zF8t4Ri2KuQJAZsTP/qYanJihMZlfQ1PLus6Ldg+JlUePENufDGrIAgpfS5C42vtco7eGu7Y28NZMKqC9f7iuD/Qeg+5oH+GZGA==";


	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 Boolean checkSign(String param, String sign) throws Exception {

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

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

	public static void main(String[] args) throws Exception {

		/******************************************  start ******************************************/
		String param       = "前台优选";

		byte[] encodedData = RSAUtils.encryptByPrivateKey(param.getBytes(), privateKey);
		System.out.println("加密后:" + new String(encodedData, "utf-8"));

		byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey);
		System.out.println("解密后:" + new String(decodedData));

		String sign = RSAUtils.sign(encodedData, privateKey);
		System.err.println("签名:" + sign);

		// String sign ="SA+ixQKSdsGa86wT8YitHp0veyy+Kep44/Tjo7Yq2NjZDp5iKhhemKlrVZzzlu3BYRbdH0wIml0jFJL5USG0u+VGYJL40GGZ33V5o9lmGsuLcLxEKgibdCL68ZbrfKArbVo1h2gYVAeZvuZ9qYdhk4oeJndVNhFO9Hx8ncG6bDc=";
		boolean status = RSAUtils.verify(encodedData, publicKey, sign);
		System.err.println("签名验证结果:" + status);


		/******************************************  start ******************************************/
//		List<Node> nodes = new ArrayList<>();
//
//		Node node1 = new Node();
//		node1.setId(1);
//		node1.setName("node1");
//		node1.setParentId(0);
//		node1.setLink(null);
//		nodes.add(node1);
//
//		Node node11 = new Node();
//		node11.setId(11);
//		node11.setName("node11");
//		node11.setParentId(1);
//		node11.setLink(null);
//		nodes.add(node11);
//
//		Node node111 = new Node();
//		node111.setId(111);
//		node111.setName("node111");
//		node111.setParentId(11);
//		node111.setLink(null);
//		nodes.add(node111);
//
//		Node node12 = new Node();
//		node12.setId(12);
//		node12.setName("node12");
//		node12.setParentId(1);
//		node12.setLink(null);
//		nodes.add(node12);
//
//		Node node2 = new Node();
//		node2.setId(2);
//		node2.setName("node2");
//		node2.setParentId(0);
//		node2.setLink(null);
//		nodes.add(node2);
//
//		Node node21 = new Node();
//		node21.setId(21);
//		node21.setName("node21");
//		node21.setParentId(2);
//		node21.setLink(null);
//		nodes.add(node21);
//
//		Node node3 = new Node();
//		node3.setId(3);
//		node3.setName("node3");
//		node3.setParentId(0);
//		node3.setLink(null);
//		nodes.add(node3);
//		List<Node> nodeList = new ArrayList<Node>();
//		for (Node node : nodes) {
//			Integer id = node.getId();
//			if (node.getParentId() == 0) {
//				node = buildTree(nodes, node);
//				nodeList.add(node);
//			}
//		}
//		int i = 0;
//		for (Node item : nodeList) {
//			System.out.println(item.toString());
//			show(item, i);
//
//		}
//		System.out.println(i);
	}


	public static void show(Node node, int num) {
		if (node.getChildrens().size() > 0) {
			for (int m = 0; m < num; m++) {
				System.out.print("---");
			}
			num++;
			for (Node item : node.getChildrens()) {
				System.out.println("-----" + item.toString());
				show(item, num);
			}
		}
	}

	public static Node buildTree(List<Node> list, Node node) {
		List<Node> children = new ArrayList<Node>();
		for (Node item : list) {
			if (item.getParentId() == node.getId()) {
				children.add(buildTree(list, item));
			}
		}
		node.setChildrens(children);
		return node;

	}
}
