https://github.com/alexaubry/crypto
Swift wrapper for CommonCrypto
https://github.com/alexaubry/crypto
commoncrypto cryptography swift
Last synced: 2 months ago
JSON representation
Swift wrapper for CommonCrypto
- Host: GitHub
- URL: https://github.com/alexaubry/crypto
- Owner: alexaubry
- License: mit
- Created: 2017-11-09T07:11:51.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-12T18:04:58.000Z (almost 8 years ago)
- Last Synced: 2025-07-19T11:06:36.502Z (3 months ago)
- Topics: commoncrypto, cryptography, swift
- Language: Swift
- Size: 95.7 KB
- Stars: 4
- Watchers: 1
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Crypto
Crypto is an easy-to-use and type safe Swift wrapper for CommonCrypto.
## Requirements
- iOS 8 and later
- macOS 10.10 and later
- tvOS 9.0 and later
- watchOS 2.0 and later
- Swift 3.2 and later## Installation
Crypto is available via Carthage.
### Carthage
To install Crypto using [Carthage](https://github.com/Carthage/Carthage), add this line to your `Cartfile`:
~~~
github "alexaubry/Crypto"
~~~### CocoaPods
Crypto cannot be installed with Carthage yet. Please read [#1](https://github.com/alexaubry/Crypto/issues/1) for more information.
## Usage
Crypto supports the following operations:
- [Digests](#digests)
- [HMAC](#hmac)
- [Key Derivation - PBKDF2](#key-derivation)
- [Symmetric Key Wrap](#key-wrap)
- [Encryption / Decryption](#encryption-and-decryption)### Digests
Use the `Digest` enum to compute the hash of messages.
~~~swift
let message: Data = "secret".data(using: .utf8)!
let hash = Digest.sha256.hash(message: message)
~~~The `hash(message:)` method returns a `Data` object containing the hash.
> Available digests: `.md4`, `.md5`, `.sha1`, `.sha224`, `.sha256`, `.sha384`, `.sha512`
### HMAC
Use the `HMAC` enum to compute the HMAC for a given message and key.
~~~swift
let message: Data = "secret".data(using: .utf8)!
let key: Data = ...
let hmac = HMAC.sha256.authenticate(message: message, with: key)
~~~The `authenticate(message:,with:)` method returns a `Data` object containing the HMAC code for the message.
> Available algorithms: `.sha1`, `.sha224`, `.sha256`, `.sha384`, `.sha512`
## Key Derivation
Use the `KeyDerivation` enum to calibrate and derive passwords with the `PBKDF2` algorithm.
## Key Wrap
Use the `SymmetricKeyWrap` enum to wrap and unwrap keys with an encryption key.
## Encryption and Decryption
Use the `Cryptor` enum to encrypt and decrypt data.
### Random Data
Two random data generators are available:
- `CommonRandom` - uses `CCRandomGenerateBytes` from CommonCrypto
- `SecRandom` - uses `SecRandomCopyBytes` from Security.frameworkThey both conform to the `Random` protocol.
**Example**:
~~~swift
let ccRandom: Data = try CommonRandom.generate(bytes: 32)
let secRandom: Data = try SecRandom.generate(bytes: 32)
~~~This generates 32 bytes of random data.
## Author
Written by Alexis Aubry. You can [find me on Twitter](https://twitter.com/_alexaubry).
## License
Crypto is available under the MIT license. See the [LICENSE](LICENSE) file for more info.