java语言分析区块链钱包生成的原理

java语言分析区块链钱包生成的原理,第1张

java语言分析区块链钱包生成的原理:

一、区块链钱包实现的技术原理用大概就是:

钱包助记词生成了种子,种子发芽结果,果实就是私钥,私钥推导出了公钥,公钥数据的节选部分成了钱包地址。同时钱包提供了Key Store,他也是私钥加密后的文件为了配合正常的密码使用,便捷用户的钱包使用。

公钥:它是密码学上的一种概念。通过一种算法得到,该算法是得出一对秘钥:公钥和私钥。公钥是属于非对称加密,是秘钥对中的公开部分。

私钥:它是密码学上的一种概念。通过一种算法得到,该算法是得出一对秘钥:公钥和私钥,私钥是秘钥对中的非公开部分,私钥的持有人是数字货币的持有者。

钱包地址:它类似于每一张yhk的卡号,一个人可以在银行通过身份z办理多张yhk,同理他也可以拥有多个钱包地址。钱包地址是这对密钥中公钥转换而来,所以一个钱包地址只能对应一个私钥。

钱包助记词的诞生是私钥太难记,但是又要保证钱包的安全性。一般情况下,助记词由一些单词组成,只要你记住这些单词,按照顺序在钱包中输入,也能打开钱包。

比特币钱包生成原理参考

二、钱包生成伪代码:

package com.blockchain.model;

import java.util.Map;

import com.blockchain.security.CryptoUtil;
import com.blockchain.security.RSACoder;

/**
 * 钱包
 * @author nandao
 */
public class Wallet {
	/**
	 * 公钥
	 */
	private String publicKey;
	/**
	 * 私钥
	 */
	private String privateKey;

	public String getPublicKey() {
		return publicKey;
	}

	public void setPublicKey(String publicKey) {
		this.publicKey = publicKey;
	}

	public String getPrivateKey() {
		return privateKey;
	}

	public void setPrivateKey(String privateKey) {
		this.privateKey = privateKey;
	}

	public Wallet() {
    }
	
	/**
	 * 只包含公钥的钱包,用来给其他节点使用,其他节点在转账时需要用到
	 * @param publicKey
	 */
	public Wallet(String publicKey) {
		this.publicKey = publicKey;
	}
	
	public Wallet(String publicKey, String privateKey) {
		this.publicKey = publicKey;
		this.privateKey = privateKey;
	}
	
	public static Wallet generateWallet() {
		Map initKey;
		try {
			// 本地生成公私钥对
			initKey = RSACoder.initKey();
			String publicKey = RSACoder.getPublicKey(initKey);
			String privateKey = RSACoder.getPrivateKey(initKey);
			return new Wallet(publicKey, privateKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取钱包地址
	 * @return
	 */
	public String getAddress() {
		String publicKeyHash = hashPubKey(publicKey);
		return CryptoUtil.MD5(publicKeyHash);   
	}

	/**
	 * 根据钱包公钥生成钱包地址
	 * @param publicKey
	 * @return
	 */
	public static String getAddress(String publicKey) {
		String publicKeyHash = hashPubKey(publicKey);
		return CryptoUtil.MD5(publicKeyHash);
	}

	/**
	 * 获取钱包公钥hash
	 * @return
	 */
	public String getHashPubKey() {
		return CryptoUtil.SHA256(publicKey);
	}

	/**
	 * 生成钱包公钥hash
	 * @param publicKey
	 * @return
	 */
	public static String hashPubKey(String publicKey) {
		return CryptoUtil.SHA256(publicKey);
	}

}

生成钱包地址的工具类:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * 加密工具类
 * @author nandao
 */
public class CryptoUtil {
	private CryptoUtil() {
	}

	public static String SHA256(String str) {
		MessageDigest messageDigest;
		String encodeStr = "";
		try {
			messageDigest = MessageDigest.getInstance("SHA-256");
			messageDigest.update(str.getBytes("UTF-8"));
			encodeStr = byte2Hex(messageDigest.digest());
		} catch (Exception e) {
			System.out.println("getSHA256 is error" + e.getMessage());
		}
		return encodeStr;
	}

	public static String MD5(String str) {  
        try {  
            StringBuffer buffer = new StringBuffer();  
            char[] chars = new char[]{'0','1','2','3',  
                    '4','5','6','7','8','9','A','B','C','D','E','F'};  
            byte [] bytes = str.getBytes();  
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");  
            byte[] targ = messageDigest.digest(bytes);  
            for(byte b:targ) {  
                buffer.append(chars[(b>>4)&0x0F]);  
                buffer.append(chars[b&0x0F]);  
            }  
            return buffer.toString();  
        } catch (NoSuchAlgorithmException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        return null;  
    } 

	public static String UUID() {
		return UUID.randomUUID().toString().replaceAll("\\-", "");
	}

	private static String byte2Hex(byte[] bytes) {
		StringBuilder builder = new StringBuilder();
		String temp;
		for (int i = 0; i < bytes.length; i++) {
			temp = Integer.toHexString(bytes[i] & 0xFF);
			if (temp.length() == 1) {
				builder.append("0");
			}
			builder.append(temp);
		}
		return builder.toString();
	}
	
}

获取钱包地址的测试代码:

	@Test
	public void testGenWallet() throws Exception {
		//获取钱包
		Wallet wallet = Wallet.generateWallet();
		//获取钱包地址
		String walletAddress = Wallet.getAddress(wallet.getPublicKey());
		System.out.println("地址钱包:"+ walletAddress);
		System.out.println(JSON.toJSON(wallet));
	}

到此、钱包地址的生成方案分享完毕。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/2991941.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-23
下一篇 2022-09-23

发表评论

登录后才能评论

评论列表(0条)

保存