使用Ed25519给数据签名(oc版)
发布网友
发布时间:2022-11-16 10:43
我来回答
共1个回答
热心网友
时间:2024-11-24 21:36
//==============BlinkEd25519.h===========//
#import <Foundation/Foundation.h>
#include "Ed25519.h"
@interfaceEd25519Keypair :NSObject
@property (nonatomic,strong) NSData *publickey;//公钥32位
@property (nonatomic,strong) NSData *privatekey;//私钥64位
@end
NS_ASSUME_NONNULL_BEGIN
@interfaceBlinkEd25519 :NSObject
/**
生成ed25519密钥串
@return Ed25519Keypair对象,保存一对密钥串
*/
+(Ed25519Keypair*)generateEd25519KeyPair;
/**
签名数据
@param ed25519keypair 密钥串
@param content 需要签名的数据(json格式的字符串)
@return 签名后的数据
*/
+(NSData*)BLinkEd25519_Signature:(Ed25519Keypair*)ed25519keypair Content:(NSString*)content;
/**
验证签名数据
@param signatureData 签名数据
@param contentData 签名前数据
@param ed25519keypair ed25519密钥串
@return 返回是否
*/
+(BOOL)BlinkEd25519_Verify:(NSData*)signatureData content:(NSData*)contentData Ed25519Keypair:(Ed25519Keypair*)ed25519keypair;
@end
//==============BlinkEd25519.m===========//
#import "BlinkEd25519.h"
@implementation Ed25519Keypair
@end
@implementation BlinkEd25519
+(Ed25519Keypair*)generateEd25519KeyPair
{
unsignedcharseed[32],publickey[32],privatekey[64];
ed25519_create_seed(seed);
ed25519_create_keypair(publickey, privatekey, seed);
Ed25519Keypair *keypair = [[Ed25519Keypair alloc] init];
keypair.publickey= [NSDatadataWithBytes:publickeylength:32];
keypair.privatekey= [NSDatadataWithBytes:privatekeylength:64];
returnkeypair;
}
+(NSData*)BLinkEd25519_Signature:(Ed25519Keypair*)ed25519keypair Content:(NSString*)content
{
unsignedcharsignature[64];
NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding];
ed25519_sign(signature, [contentDatabytes], contentData.length, [ed25519keypair.publickeybytes], [ed25519keypair.privatekeybytes]);
return[NSDatadataWithBytes:signaturelength:64];
}
+(BOOL)BlinkEd25519_Verify:(NSData*)signatureData content:(NSData*)contentData Ed25519Keypair:(Ed25519Keypair*)ed25519keypair
{
returned25519_verify([signatureDatabytes], [contentDatabytes], contentData.length, [ed25519keypair.privatekeybytes]);
}
@end