-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
564 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// SHA256.h | ||
// GigaBitcoin/secp256k1.swift | ||
// | ||
// Copyright (c) 2021 GigaBitcoin LLC | ||
// Distributed under the MIT software license | ||
// | ||
// See the accompanying file LICENSE for information | ||
// | ||
|
||
#include "../secp256k1/src/hash.h" | ||
#include "../secp256k1/include/secp256k1.h" | ||
|
||
/// Exposes secp256k1 SHA256 implementation to the bindings target | ||
/// @param output pointer to an array to be filled by the function | ||
/// @param input a pointer to the data to be hashed | ||
/// @param len the length of the data to be hashed | ||
void secp256k1_swift_sha256(unsigned char *output, const unsigned char *input, size_t len); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// SHA256.c | ||
// GigaBitcoin/secp256k1.swift | ||
// | ||
// Copyright (c) 2021 GigaBitcoin LLC | ||
// Distributed under the MIT software license | ||
// | ||
// See the accompanying file LICENSE for information | ||
// | ||
|
||
#include "SHA256.h" | ||
#include "../secp256k1/src/hash_impl.h" | ||
|
||
/// Exposes secp256k1 SHA256 implementation to the bindings target | ||
/// @param output pointer to an array to be filled by the function | ||
/// @param input a pointer to the data to be hashed | ||
/// @param len the length of the data to be hashed | ||
void secp256k1_swift_sha256(unsigned char *output, const unsigned char *input, size_t len) { | ||
secp256k1_sha256 hasher; | ||
secp256k1_sha256_initialize(&hasher); | ||
secp256k1_sha256_write(&hasher, input, len); | ||
secp256k1_sha256_finalize(&hasher, output); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// Digests.swift | ||
// GigaBitcoin/secp256k1.swift | ||
// | ||
// Modifications Copyright (c) 2021 GigaBitcoin LLC | ||
// Distributed under the MIT software license | ||
// | ||
// See the accompanying file LICENSE for information | ||
// | ||
// | ||
// NOTICE: THIS FILE HAS BEEN MODIFIED BY GigaBitcoin LLC | ||
// UNDER COMPLIANCE WITH THE APACHE 2.0 LICENSE FROM THE | ||
// ORIGINAL WORK OF THE COMPANY Apple Inc. | ||
// | ||
// THE FOLLOWING IS THE COPYRIGHT OF THE ORIGINAL DOCUMENT: | ||
// | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftCrypto open source project | ||
// | ||
// Copyright (c) 2019-2020 Apple Inc. and the SwiftCrypto project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.md for the list of SwiftCrypto project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
// MARK: - SHA256Digest + DigestPrivate | ||
|
||
public struct SHA256Digest: Digest { | ||
let bytes: (UInt64, UInt64, UInt64, UInt64) | ||
|
||
public static var byteCount: Int { | ||
get { return 32 } | ||
|
||
set { fatalError("Cannot set SHA256.byteCount") } | ||
} | ||
|
||
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { | ||
return try Swift.withUnsafeBytes(of: bytes) { | ||
let boundsCheckedPtr = UnsafeRawBufferPointer(start: $0.baseAddress, | ||
count: Self.byteCount) | ||
return try body(boundsCheckedPtr) | ||
} | ||
} | ||
|
||
private func toArray() -> ArraySlice<UInt8> { | ||
var array = [UInt8]() | ||
array.appendByte(bytes.0) | ||
array.appendByte(bytes.1) | ||
array.appendByte(bytes.2) | ||
array.appendByte(bytes.3) | ||
return array.prefix(upTo: SHA256Digest.byteCount) | ||
} | ||
|
||
public var description: String { | ||
return "\("SHA256") digest: \(toArray().hexString)" | ||
} | ||
|
||
public func hash(into hasher: inout Hasher) { | ||
self.withUnsafeBytes { hasher.combine(bytes: $0) } | ||
} | ||
} |
Oops, something went wrong.