An easy way for hashing and encryption.

Overview

Logo

Language Support Platform License

Carthage compatible CocoaPods Version

Travis CI Status Codebeat Codecov Beerpay

CatCrypto include a series of hashing and encryption functions and more functions in progress!

CatCrypto also contains Swift bindings of Argon2, the password-hashing function that won the Password Hashing Competition (PHC).

Content

Requirements

  • Swift 4+
  • iOS 8.0+
  • macOS 10.10+
  • tvOS 9.0+
  • watchOS 2.0+

Support Functions

Upcoming Functions

  • Advanced Encryption Standard (AES)
  • Data Encryption Standard (DES)
  • Triple DES (3DES)

Usage

Context

Context contains inputs and configures for function crypto.

Change hash length with SHA-2 function crypto:

let sha2Crypto = CatSHA2Crypto()
sha2Crypto.context.hashLength = .bit384

Hashing

Hash function used to map data of arbitrary size to data of fixed size.

Simply hashing string with MD6 function crypto:

let md6Crypto = CatMD6Crypto()
md6Crypto.context.hashLength = .bit512
print(md6Crypto.hash(password: "CatCrypto").hexStringValue())

// 3ad3003383633c40281bb5185424ee56a5a1c6dfa3a0e7c3a9e381c58d253323e146feb3f04cb9ebcde47186e042ce63109b8d19f3ca760ea00c90654eb2b272

Verification

Some hash function support to verify their hashed value.

Verifing with Argon2 function crypto:

let hash = "$argon2i$v=19$m=4096,t=3,p=1$Q2F0Q3J5cHRv$Ad6gXMVLvZ3uQOeTi6nCmU4Ns2/nPDfPD5B3yyebv8k"
let argon2Crypto = CatArgon2Crypto()
argon2Crypto.context.mode = .argon2i
argon2Crypto.context.salt = "CatCrypto"
print(argon2Crypto.verify(hash: hash, password: "CatCrypto").boolValue())

// true

Installation

CatCrypto is available through CocoaPods and Carthage.

CocoaPods

Add the following line to your Podfile:

use_frameworks!

pod 'CatCrypto'

Carthage

Add the following line to your Cartfile:

github "ImKcat/CatCrypto"

Documentation

Interacting

CatCrypto is always trying to support more functions and keep itself easy to use, please reading down below to interacting with CatCrypto.

Need Help

Contribute

If you want to contribute with CatCrypto, please reading Contribute Guidelines at first.

License

CatCrypto is available under the MIT license. See the LICENSE file for more info.

Comments
  • iOS 13 Support

    iOS 13 Support

    Environment

    • CatCrypto: 0.3.0
    • Xcode: 11.0 beta 5
    • iOS: 13.0 beta 6

    Reproduce steps

    1. Build a project.

    Description

    Swift Compiler Error:

    • Redefinition of module 'CommonCrypto' (iPhoneSimulator13.0.sdk/usr/include/CommonCrypto/module.modulemap:1:8)
    • Could not build Objective-C module 'CommonCryptoFramework' (CatCrypto/Sources/Core/Crypto.swift:23:8)
    opened by olegbarinov 5
  • Argon2 result with \0 at the end

    Argon2 result with \0 at the end

    Issue type

    • [ ] Bug
    • [ ] Feature
    • [X] Question

    Before question

    Desciption

    After updating to 0.3.0 I noticed null char at the end of result string when using Argon2 hash function.

    i.e. Even when checking CatCrypto tests (ArgonTests.swift) I expect to get

    "$argon2i$v=19$m=4096,t=3,p=1$ODExM0FENzYtQjFENC00NzE0LUE1NjktQTkyMDMzNkYzRkI3$wJfIvpKfuE91unyYNwEfLyuaXhWS9XuTJOGaFbP7wOE"

    and I get this instead:

    "$argon2i$v=19$m=4096,t=3,p=1$ODExM0FENzYtQjFENC00NzE0LUE1NjktQTkyMDMzNkYzRkI3$wJfIvpKfuE91unyYNwEfLyuaXhWS9XuTJOGaFbP7wOE\0"

    Is that expected behaviour?

    arg

    enhancement 
    opened by SimonasA 5
  • Argon2 hexStringValue seems incorrect

    Argon2 hexStringValue seems incorrect

    Issue type

    • [x] Bug
    • [ ] Feature
    • [ ] Question

    Environment

    • CatCrypto: 0.3.2
    • Xcode: 12.1
    • iOS: 14.1

    Description

    Currently using CatCrypto for my downstream library, react-native-argon2 and this is a replacement for the SignalApp library as that has a GPL license and I would like to keep my package as MIT. Hence I chose your library. However I seem to be getting an incorrect hexadecimal value from the output of the Argon2 hashing. The string representation is correct however the hexadecimal value doesn't seem to be right.

    Reproduce steps

    Password: password Salt: 1234567891011121314151617181920212223242526272829303132333435363

    Code:

        let argon2Context = CatArgon2Context.init();
        argon2Context.iterations = 2;
        argon2Context.memory = 32 * 1024;
        argon2Context.parallelism = 1;
        argon2Context.salt = salt;
        argon2Context.hashLength = 32;
        argon2Context.mode = .argon2id;
    
        let argon2Crypto = CatArgon2Crypto.init(context: argon2Context);
        let result = argon2Crypto.hash(password: password);
    
        if ((result.error) != nil) {
            let error = NSError(domain: "com.poowf.argon2", code: 200, userInfo: ["Error reason": "Failed to generate argon2 hash"])
            reject("E_ARGON2", "Failed to generate argon2 hash", error)
        }
    
        let rawHash = result.hexStringValue();
        let encodedHash = result.stringValue();
    
        let resultDictionary: NSDictionary = [
            "rawHash" : rawHash,
            "encodedHash" : encodedHash,
        ]
    

    Expected: Hexadecimal value: 031d6c82ddede1200f4794605052745dd562bd4db358e23dac1b11c052eff8d9 String value: $argon2id$v=19$m=32768,t=2,p=1$MTIzNDU2Nzg5MTAxMTEyMTMxNDE1MTYxNzE4MTkyMDIxMjIyMzI0MjUyNjI3MjgyOTMwMzEzMjMzMzQzNTM2Mw$Ax1sgt3t4SAPR5RgUFJ0XdVivU2zWOI9rBsRwFLv+Nk

    Received: Hexadecimal value: 246172676f6e32696424763d3139246d3d33323736382c743d322c703d31244d54497a4e4455324e7a67354d5441784d5445794d544d784e4445314d5459784e7a45344d546b794d4449784d6a49794d7a49304d6a55794e6a49334d6a67794f544d774d7a457a4d6a4d7a4d7a517a4e544d324d77244178317367743374345341505235526755464a30586456697655327a574f49397242735277464c762b4e6b00 String value: $argon2id$v=19$m=32768,t=2,p=1$MTIzNDU2Nzg5MTAxMTEyMTMxNDE1MTYxNzE4MTkyMDIxMjIyMzI0MjUyNjI3MjgyOTMwMzEzMjMzMzQzNTM2Mw$Ax1sgt3t4SAPR5RgUFJ0XdVivU2zWOI9rBsRwFLv+Nk

    opened by zanechua 3
  • Dynamically generate CommonCrypto module maps

    Dynamically generate CommonCrypto module maps

    Added

    Added script to dynamically generate CommonCrypto module maps if they don't exist.

    This is based on https://stackoverflow.com/a/42852743/3188334

    Instead of using pre-created module maps, this script will generate them at build time if needed. Since Xcode 10 includes the module maps for CommonCrypto, these will not be generated if they already exist. Also, dynamically generating the module maps allows users of the library to build with beta versions of Xcode or on CI providers that may have Xcode in a non-standard location, since paths like the one below are no longer hardcoded: https://github.com/ImKcat/CatCrypto/blob/4c3e6b389f80c3fb9d873e039b10cb69dbbde34e/Sources/ModuleMaps/CommonCrypto/iPhoneOS/module.modulemap#L2

    This was tested on Xcode 10.2 beta and passes Travis CI tests for Xcode 10, but has not been tested on Xcode 9 (where these module maps are not already included in the SDK)

    opened by rajivshah3 2
  • Argon random uint8 salt

    Argon random uint8 salt

    Issue type

    • [X ] Bug
    • [X ] Feature
    • [ ] Question