A tiny and easy to use Swift class to encrypt strings using HMAC algorithms.

Related tags

Encryption SweetHMAC
Overview

#Sweet HMAC

Twitter: @jancassio License Build Version Platforms

SweetHMAC is a tiny and easy to use Swift class to encrypt strings using HMAC algorithms. A special thanks to jernejstrasner for shared HMACDigest Gist, that inspired to create this simple class and String extension.

Usage examples

There are two ways to use Sweet HMAC in your projects

1. by String extension

// Will output this string: e470f785afb708cd8c2a31860642fd11
"I'm going to make him an offer he can't refuse".HMAC(.md5, secret:"Vito Corleone")

2. by SweetHMAC class

let quote = "I'm going to make him an offer he can't refuse"
let author = "Vito Corleone"

// Create a SweetHMAC instance with your message and secret strings
let digest:SweetHMAC = SweetHMAC(message: quote, secret: author)

// Pick some computed HMAC output based on some algorithm using "HMAC" method...
let md5 = digest.HMAC(algorithm: .md5)

// ...or do it more "Sweet" like this
let md5 = SweetHMAC(message: quote, secret: author).HMAC(.MD5)

Supported HMAC algorithms

  • MD5
  • SHA1
  • SHA224
  • SHA256
  • SHA384
  • SHA512

Installation

SweetHMAC have many clear and simple options to be used in any iOS or OSX projects.

##Using dependency manager Actually SweetHMAC can be used with those dependency managers

  • CocoaPods
  • Carthage

###CocoaPods

You can use SweetHMAC with CocoaPods, specify your Podfile like this:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!

pod 'SweetHMAC', '~> 3.0'

##Carthage

Also you can use Carthage to SweetHMAC framework at your project, just add SweetHMac in your Cartfile file like this:

github "jancassio/SweetHMAC" >= 3.0

##Integrated Project

As many iOS/OSX projects, you can add third party libraries in your project as well.

###Embeded Framework

You can add SweetHMAC as submodule (preferred) or paste entire SweetHMAC project relative to your Xcode project, choose one of both options and after them:

  1. Drag SweetHMAC.xcodeproj from SweetHMAC folder to your project.
  2. At Xcode, open "Project Navigator" (⌘+1).
  3. Select SweetHMAC project (the blue project icon).
  4. Check if SweetHMAC deployment target matches your project deployment target.
  5. At this point, select "Build Phases" tab.
  6. Click on + icon to add a new "Copy Phase". Optional: Rename this phase to "Embeded Frameworks".
  7. Set destination to "Frameworks".
  8. Add SweetHMAC.framework.

Source file

This is not the preferred option to add SweetHMAC in your project, because SweetHMAC depends by CommonCrypto lib to work properly and, Swift can't access this lib directly.

So if you still want to use SweetHMAC by source files, follow steps below:

  1. At Xcode, open "Project Navigator" (⌘+1).
  2. Select your project file (the blue project icon).
  3. Click on "Build Phases" tab.
  4. Create a new "Run Script" phase clicking on + icon.
  5. Drag the run script phase created to stay below the first phase (ak "Target dependencies").
  6. Paste the code below at run script phase created before:
COMMOM_CRYPTO_PATH=$SDKROOT/usr/include/CommonCrypto/CommonCrypto.h
COMMOM_CRYPTO_R_PATH=$SDKROOT/usr/include/CommonCrypto/CommonRandom.h

MODULE_DIR="$SRCROOT/Modules/CommonCrypto"
MODULE_FILE=$MODULE_DIR/module.map
MODULE_TEMPLATE="module CommonCrypto [system] {\n\t
  header \"$COMMOM_CRYPTO_PATH\"\n\t
  header \"$COMMOM_CRYPTO_R_PATH\"\n\t
  export *\n
}"

echo "Create Modules path to map CommonCrypto lib"
mkdir -p "$SRCROOT/Modules/CommonCrypto"

echo "Cleanup previous CommonCrypto script to make sure the deployment target is always updated"

echo "" > $MODULE_FILE

echo "Create CommonCrypto module map template"
echo -e $MODULE_TEMPLATE > $MODULE_FILE
  1. Create a new file (⌘+N).
  2. Select "iOS"/"Others".
  3. Select "Congfiguration Settings File".
  4. Put any name you want.
  5. Add the content below in Configuration file created before:
SWIFT_INCLUDE_PATHS="$SRCROOT/Modules"
  1. Go to your project file in "Project Navigator" (⌘+1)
  2. Select your project file and select your project above your project targets.
  3. Change configurations for the last one you created for each target you are using SweetHMAC by source.
  4. Build your project.

###Quick Observation Each time you change your deployment device for example, from Simulator to Device, you should build your project, because the script added in build phase, will use the current operational system selected in your Xcode scheme to construct the absolute path to the CommonCrypto header relative of Simulator SDK or iPhoneOS SDK.

License

Copyright (c) 2014 Jan Cassio. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

You might also like...
Util for generation RSA keys on your client and save to keychain or convert into Data 🔑 🔐

RSASwiftGenerator 🔑 🔐 To run the example project, clone the repo, and run pod install from the Example directory first. Requirements ⚠️ SWIFT 4 XCod

BitWiser - A simple library to help you in dealing with bytes, bits and nibbles
BitWiser - A simple library to help you in dealing with bytes, bits and nibbles

BitWiser Bitwiser is a collection of methods and properties that makes you work

A KeePass/Password Safe Client for iOS and OS X

Strongbox A Personal Password Manager for iOS & OSX that can be found on the Apple App Store here: https://apps.apple.com/app/strongbox-password-safe/

A wrapper for Apple's Common Crypto library written in Swift.

IDZSwiftCommonCrypto A Swift wrapper for Apple's CommonCrypto library. IDZSwiftCommonCrypto works with both CocoaPods and Cathage. For more details on

Read my answer here Importing CommonCrypto in a Swift framework

Read my answer here Importing CommonCrypto in a Swift framework

A pure Swift implementation of MD5

SwiftMD5 SwiftMD5 is a pure Swift implementation for the MD5 algorithm. Usage import SwiftMD5 "The quick brown fox jumps over the lazy dog".md5 // "9

🍕 MD5 in pure Swift
🍕 MD5 in pure Swift

SwiftHash ❤️ Support my app ❤️ Push Hero - pure Swift native macOS application to test push notifications Quick Access - Organise files in the Mac men

RSA public/private key encryption in Swift

SwiftyRSA Maintainer(s): @ikeith Public key RSA encryption in Swift. SwiftyRSA is used in the Scoop iOS app to encrypt driver license numbers before s

Enigma encryption in Swift
Enigma encryption in Swift

𝕰𝖓𝖎𝖌𝖒𝖆𝕶𝖎𝖙 𝖀𝖘𝖆𝖌𝖊 // Will create an Enigma with an empty plugboard, rotors I, II, III and reflector B (wide). let enigma = Enigma() // Wil

Comments
  • Update .travis file so far Travis CI support Xcode 6.3

    Update .travis file so far Travis CI support Xcode 6.3

    Just waiting to Travis update infrastructure to run Xcode 6.3 properly since this update runs only in OS X 10.10.3

    There is the thread about this issue at Travis CI

    opened by jancassio 0
  • None of your spec sources contain a spec satisfying the dependency: `SweetHMAC (~> 1.1)`.

    None of your spec sources contain a spec satisfying the dependency: `SweetHMAC (~> 1.1)`.

    I am getting the following message when i try to install the cocoapod

    Analyzing dependencies [!] Unable to satisfy the following requirements:

    • SweetHMAC (~> 1.1) required by Podfile
    • SweetHMAC (~> 1.1) required by Podfile

    None of your spec sources contain a spec satisfying the dependency: SweetHMAC (~> 1.1).

    You have either:

    • out-of-date source repos which you can update with pod repo update.
    • mistyped the name or version.
    • not added the source repo that hosts the Podspec to your Podfile.

    Note: as of CocoaPods 1.0, pod repo update does not happen on pod install by default.

    opened by ashiqsulaiman 3
  • SweetHMAC 1.1 not in Cocoapods

    SweetHMAC 1.1 not in Cocoapods

    Hi there,

    I have modified my Podfile to include pod 'SweetHMAC', '~> 1.1' however I still get an error that says

    [!] Unable to satisfy the following requirements:
    
    - 'SweetHMAC (~> 1.1)' required by 'Podfile'
    

    When running pod install. It seems SweetHMAC 1.1 is not updated in Cocoapods as if I change it to 1.0, it works (however then I have problems with Common Crypto).

    Could you help with this please?

    Thanks!

    opened by GavinPacini 2
  • CommonCrypto not validate in SweetHMAC.podspec

    CommonCrypto not validate in SweetHMAC.podspec

    My cocoapods version is 0.39.0

    when I use pod validate SweetHMAC.podspec

    pod spec lint SweetHMAC.podspec
    

    I got these errors:

    -> SweetHMAC (1.1)
    - ERROR | xcodebuild: Returned an unsuccessful exit code.
    - WARN  | xcodebuild:  SweetHMAC/SweetHMAC/Source/SweetHMAC.swift:32:8: warning: file 'SweetHMAC.swift' is part of module 'CommonCrypto'; ignoring import
    - ERROR | xcodebuild:  SweetHMAC/SweetHMAC/Source/SweetHMAC.swift:79:23: error: use of undeclared type 'CCHmacAlgorithm'
    - ERROR | xcodebuild:  SweetHMAC/SweetHMAC/Source/SweetHMAC.swift:104:19: error: use of unresolved identifier 'CC_MD5_DIGEST_LENGTH'
    - ERROR | xcodebuild:  SweetHMAC/SweetHMAC/Source/SweetHMAC.swift:107:19: error: use of unresolved identifier 'CC_SHA1_DIGEST_LENGTH'
    - ERROR | xcodebuild:  SweetHMAC/SweetHMAC/Source/SweetHMAC.swift:110:19: error: use of unresolved identifier 'CC_SHA224_DIGEST_LENGTH'
    - ERROR | xcodebuild:  SweetHMAC/SweetHMAC/Source/SweetHMAC.swift:113:19: error: use of unresolved identifier 'CC_SHA256_DIGEST_LENGTH'
    - ERROR | xcodebuild:  SweetHMAC/SweetHMAC/Source/SweetHMAC.swift:116:19: error: use of unresolved identifier 'CC_SHA384_DIGEST_LENGTH'
    - ERROR | xcodebuild:  SweetHMAC/SweetHMAC/Source/SweetHMAC.swift:119:19: error: use of unresolved identifier 'CC_SHA512_DIGEST_LENGTH'
    - ERROR | xcodebuild:  SweetHMAC/SweetHMAC/Source/SweetHMAC.swift:169:5: error: use of unresolved identifier 'CCHmac'
    
    Analyzed 1 podspec.
    
    [!] The spec did not pass validation, due to 9 errors and 1 warning.
    
    bug 
    opened by kingiol 2
Owner
Jan Cássio
Self-taught programmer, everyday learner, music and coffee powered.
Jan Cássio
CryptoSwift is a growing collection of standard and secure cryptographic algorithms implemented in Swift

CryptoSwift Crypto related functions and helpers for Swift implemented in Swift. (#PureSwift) Note: The main branch follows the latest currently relea

Marcin Krzyzanowski 9.4k Jan 9, 2023
Secure your app by obfuscating all the hard-coded security-sensitive strings.

App Obfuscator for iOS Apps Secure your app by obfuscating all the hard-coded security-sensitive strings. Security Sensitive strings can be: REST API

pj 601 Dec 16, 2022
Safe and easy to use crypto for iOS and macOS

Swift-Sodium Swift-Sodium provides a safe and easy to use interface to perform common cryptographic operations on macOS, iOS, tvOS and watchOS. It lev

Frank Denis 483 Jan 5, 2023
Easy to use cryptographic framework for data protection: secure messaging with forward secrecy and secure data storage. Has unified APIs across 14 platforms.

Themis provides strong, usable cryptography for busy people General purpose cryptographic library for storage and messaging for iOS (Swift, Obj-C), An

Cossack Labs 1.6k Dec 30, 2022
An easy way for hashing and encryption.

CatCrypto include a series of hashing and encryption functions and more functions in progress! CatCrypto also contains Swift bindings of Argon2, the p

Kcat 62 Sep 27, 2022
Demonstration library for using the Secure Enclave on iOS

SecureEnclaveCrypto This project shows you how to create a keypair where as the private key is stored in the secure enclave sign a string / some data

Trail of Bits 272 Jan 7, 2023
RSA public/private key generation, RSA, AES encryption/decryption, RSA sign/verify in Swift with CommonCrypto in iOS and OS X

SwCrypt Create public and private RSA keys in DER format let (privateKey, publicKey) = try! CC.RSA.generateKeyPair(2048) Convert them to PEM format l

soyer 695 Dec 8, 2022
Simple and secure hashing in Swift with the SipHash algorithm

SipHash ⚠️ WARNING This package has been obsoleted by the Hasher type and the Hashable.hash(into:) requirement introduced in Swift 4.2. Using this pac

null 262 Dec 19, 2022
CCCryptor (AES encryption) wrappers for iOS and Mac in Swift. -- For ObjC, see RNCryptor/RNCryptor-objc

RNCryptor Cross-language AES Encryptor/Decryptor data format. The primary targets are Swift and Objective-C, but implementations are available in C, C

null 3.3k Jan 7, 2023
A framework for the JOSE standards JWS, JWE, and JWK written in Swift.

JOSESwift is a modular and extensible framework for the JOSE standards JWS, JWE, and JWK written in Swift. ?? Please note that this implementation of

Airside Mobile, Inc. 162 Dec 15, 2022