美文网首页
iOS (区块链学习)-使用ed25519创建公私钥对

iOS (区块链学习)-使用ed25519创建公私钥对

作者: AngryProject | 来源:发表于2020-11-10 16:38 被阅读0次

    1.使用cocoapods 导入两个库

    
        pod 'CoreBitcoin'
    
        pod 'libsodium'
    
    

    2.使用ed25519生成随机公私钥对

    需要引入的头文件

    
    #import "crypto_sign_ed25519.h"
    
    #import "BTCBase58.h"
    
    
    
    unsigned char seed[32],publickey[32],privatekey[64];
    
        int i = crypto_sign_ed25519_keypair(publickey,privatekey);
    
        NSData * publickey_data= [NSData dataWithBytes:publickeylength:32];
    
        NSData * privatekey_data= [NSData dataWithBytes:privatekeylength:64];
    
        NSString *privatekey =BTCBase58StringWithData(privatekey_data);
    
        NSString *publickey =BTCBase58StringWithData(publickey_data);
    
    

    3.验证 (可使用tp钱包 IOST账号创建那里验证)

    image

    4. 私钥创建公钥方法

    
    + (NSString*)getPublicByPrivateKey:(NSString*)privatekey;
    
    {
    
        NSData*privatekeyDecode =BTCDataFromBase58(privatekey);
    
        if((privatekeyDecode.length!=32) && (privatekeyDecode.length!=64))
    
        {
    
           return;
    
        }
    
        Byte codeKeyByteAry[privatekeyDecode.length];
    
        for(inti =0; i < privatekeyDecode.length; i++) {
    
            NSData*idata = [privatekeyDecode subdataWithRange:NSMakeRange(i,1)];
    
            codeKeyByteAry[i] =((Byte*)[idata bytes])[0];
    
        }
    
        unsigned char private_key_bytes = (unsigned char)codeKeyByteAry;
    
        unsigned char public_key[32];
    
        ge_p3A;
    
        private_key_bytes[0] &=248;
    
        private_key_bytes[31] &=63;
    
        private_key_bytes[31] |=64;
    
        ge_scalarmult_base(&A, private_key_bytes);
    
        ge_p3_tobytes(public_key, &A);
    
        memmove(public_key , private_key_bytes +32,32);
    
        NSData*publickeyData= [NSData dataWithBytes:public_keylength:32];
    
        NSString*publickey =BTCBase58StringWithData(publickeyData);
    
        return publickey;
    
    }
    
    

    相关文章

      网友评论

          本文标题:iOS (区块链学习)-使用ed25519创建公私钥对

          本文链接:https://www.haomeiwen.com/subject/fgujbktx.html