区块链研习社区块链研究金马带你定投区块链

在 iOS 中实现区块链 OC版本

2018-02-02  本文已影响595人  IT

参照网上的 swift 版写了个OC版本,欢迎交流
刚搞了个QYB地址,木有BB,有木有大神打赏点玩玩
QfPddnv9jLtMa5Qt4nK1g3kveiFdmSMJBB

//
//  ViewController.m
//  BlockChainDemo
//
//  Created by Couchsurfing on 2018/2/2.
//  Copyright © 2018年 Couchsurfing. All rights reserved.
//

#import "ViewController.h"
#import "HaokBlock.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    HaokBlockChain *blockchain = [[HaokBlockChain alloc]init];
    
    for (int i = 0; i < 1024; i++) {
        HaokBlock *block =  [[HaokBlock alloc]init];
        block.data = [NSString stringWithFormat:@"%d",i];
        [blockchain addBlock:block];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
//
//  HaokBlock.h
//  BlockChainDemo
//
//  Created by Couchsurfing on 2018/2/2.
//  Copyright © 2018年 Couchsurfing. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface HaokBlock : NSObject

@property (nonatomic, assign) int index;
@property (nonatomic,copy) NSString *dateCreated;
@property (nonatomic, copy) NSString *previousHash;
@property (nonatomic, copy) NSString *blockHash;
@property (nonatomic, assign) int nonce;
@property (nonatomic, copy) NSString *data;
@property (nonatomic, copy,readonly) NSString *key;

@end


@interface HaokBlockChain : NSObject

@property (nonatomic, strong) NSMutableArray<HaokBlock *> *blocks;

- (void)addBlock:(HaokBlock *)block;

@end
//
//  HaokBlock.m
//  BlockChainDemo
//
//  Created by Couchsurfing on 2018/2/2.
//  Copyright © 2018年 Couchsurfing. All rights reserved.
//

#import "HaokBlock.h"
#import <CommonCrypto/CommonDigest.h>

@implementation HaokBlock

- (NSString *)key{
    return [NSString stringWithFormat:
            @"%d%@%@%@%d",self.index,self.dateCreated,self.previousHash,self.data,self.nonce];
}

- (instancetype)init{
    if (self = [super init]) {
        self.dateCreated = [self dateDescription];
        self.nonce = 0;
        self.data = @"";
    }
    return self;
}

- (NSString *)dateDescription{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
   return [formatter stringFromDate:[NSDate date]];
}

@end


@implementation HaokBlockChain

- (void)addBlock:(HaokBlock *)block{
    if (_blocks == nil) {
        // 添加创世区块
        // 第一个区块没有 previous hash
        block.previousHash = @"0";
        block.blockHash = [self generateHashForblock:block];
    }else{
        HaokBlock *previousBlock = [self getPreviousBlock];
        block.previousHash = previousBlock.blockHash;
        block.index = @(self.blocks.count).intValue;
        block.blockHash = [self generateHashForblock:block];
    }
    [self.blocks addObject:block];
    [self displayBlock:block];
}

- (HaokBlock *)getPreviousBlock{
    return self.blocks[self.blocks.count - 1];
}

- (void)displayBlock:(HaokBlock *)block{
    NSLog(@"%@", [NSString stringWithFormat:@"------ 第 (%d) 个区块 --------",block.index]);
    NSLog(@"%@", [NSString stringWithFormat:@"创建日期:%@",block.dateCreated]);
    NSLog(@"%@", [NSString stringWithFormat:@"数据:%@",block.data]);
    NSLog(@"%@", [NSString stringWithFormat:@"Nonce:%d",block.nonce]);
    NSLog(@"%@", [NSString stringWithFormat:@"前一个区块的哈希值:%@",block.previousHash]);
    NSLog(@"%@", [NSString stringWithFormat:@"哈希值:%@",block.blockHash]);
}

- (NSString *)generateHashForblock:(HaokBlock *)block{
    NSString *hash = [self sha1Hash:block.key];
    while (![hash hasPrefix:@"00"]) {
        block.nonce += 1;
        hash = [self sha1Hash:block.key];
    }
    return hash;
}

//sha1加密方式
- (NSString *)sha1Hash:(NSString *)input
{
    const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:input.length];
    
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1(data.bytes, (unsigned int)data.length, digest);
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    
    for(int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
        [output appendFormat:@"%02x", digest[i]];
    }
    return output;
}

- (NSMutableArray<HaokBlock *> *)blocks{
    if (_blocks == nil) {
        _blocks = [NSMutableArray array];
    }
    return _blocks;
}

@end

上一篇 下一篇

猜你喜欢

热点阅读