Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

Blake2b Algorithm

by fragi
February 28, 2022
in Algorithms, Crypto
Share on FacebookShare on Twitter

Blake2 is a cryptographic hash function. https://www.blake2.net/

Let’s now explore how to implement this algorithm in Swift. The first option is to find the test vectors and implement the algorithm in Swift. A second option is to find reliable c code and wrap it in a swift package. (http://trikalabs.com/how-to-wrap-c-code-in-a-swift-package/)
In our Cardano wallet side project we explore the second option. We use the c code from https://github.com/BLAKE2/BLAKE2 and we wrap in a swift package. 
https://github.com/arnot-project/swift-blake2b-224

We grabbed also the opportunity to provide a simpler api and handling the memory management also.

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import Foundation
import Blake2bC
 
public struct Blake2b {
    public init() {}
    
    public func computeHash(_ input: [UInt8]) -> [UInt8] {
        let capacity = 28
        let inputPointer = UnsafeMutablePointer<UInt8>.allocate(capacity: input.count)
        let output = UnsafeMutablePointer<UInt8>.allocate(capacity: capacity)
        var pkCopy: [UInt8] = Array(repeating: 0, count: capacity)
        
        for i in 0..<input.count {
            (inputPointer + i).initialize(to: input[i])
        }
        for i in 0..<capacity {
            (output + i).initialize(to: 0)
        }
        
        blake2b(output, capacity, inputPointer, input.count, nil, 0)
        
        for index in 0..<pkCopy.count {
            pkCopy[index] = (output + index).pointee
        }
        
        output.deallocate()
        inputPointer.deallocate()
        
        return pkCopy
    }
 
}

 

 

fragi

fragi

Related Posts

Swift – Disjoint Set (Union-Find)
Algorithms

Swift – Disjoint Set (Union-Find)

February 21, 2024

Disjoint Set is an algorithm that can help us solve many graph problems. There are many variations/improvements on the base...

Blake2b Algorithm
Algorithms

Operators precedence

March 2, 2022

Operators precedence matters when we have many operators in one expression. The operator with the highest precedence will be evaluated...

Blake2b Algorithm
Algorithms

Bech32 Algorithm

March 1, 2022

Bech32 is a checksummed base32 format. It is being used in cryptocurrencies development (bitcoin, cardano,...) https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki, https://cips.cardano.org/cips/cip19/. Let's explore the...

Memoization
Algorithms

Memoization

February 15, 2022

Memoization is a technique to increase the time performance of an algorithm by using some extra memory. According to Wikipedia:...

Performance testing in Xcode
Algorithms

Performance testing in Xcode

February 15, 2022

The performance is most of the times very important. In this post we will explore we can do performance testing...

How to wrap c code in a swift package
Crypto

How to wrap c code in a swift package

February 12, 2022

Sometimes we need to add c code in a Swift Package so we can use it in swift. One of...

Next Post
Blake2b Algorithm

Bech32 Algorithm

Blake2b Algorithm

Operators precedence

Delete Derived Data – zsh command

Delete Derived Data - zsh command

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.