Skip to content

Commit

Permalink
✨ Add SHA1
Browse files Browse the repository at this point in the history
  • Loading branch information
usatie committed Aug 8, 2018
1 parent 0bc4ede commit 665a216
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions BitcoinCashKit/Core/BitcoinCashKitPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
NS_ASSUME_NONNULL_BEGIN
@interface _Hash : NSObject

+ (NSData *)sha1:(NSData *)data;
+ (NSData *)sha256:(NSData *)data;
+ (NSData *)ripemd160:(NSData *)data;
+ (NSData *)hmacsha512:(NSData *)data key:(NSData *)key;
Expand Down
6 changes: 6 additions & 0 deletions BitcoinCashKit/Core/BitcoinCashKitPrivate.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@

@implementation _Hash

+ (NSData *)sha1:(NSData *)data {
NSMutableData *result = [NSMutableData dataWithLength:SHA512_DIGEST_LENGTH];
SHA1(data.bytes, data.length, result.mutableBytes);
return result;
}

+ (NSData *)sha256:(NSData *)data {
NSMutableData *result = [NSMutableData dataWithLength:SHA256_DIGEST_LENGTH];
SHA256(data.bytes, data.length, result.mutableBytes);
Expand Down
4 changes: 4 additions & 0 deletions BitcoinCashKit/Core/Crypto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import BitcoinCashKit.Private
import secp256k1

public struct Crypto {
public static func sha1(_ data: Data) -> Data {
return _Hash.sha1(data)
}

public static func sha256(_ data: Data) -> Data {
return _Hash.sha256(data)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ public struct OpSha1: OpCodeProtocol {
public func mainProcess(_ context: ScriptExecutionContext) throws {
try context.assertStackHeightGreaterThan(1)

// TODO: Implement Crypto.sha1()
//let data: Data = context.stack.removeLast()
//let hash: Data = Crypto.sha1(data)
//context.stack.append(hash)
let data: Data = context.stack.removeLast()
let hash: Data = Crypto.sha1(data)
context.stack.append(hash)
}
}

0 comments on commit 665a216

Please sign in to comment.