Simple Swift Token Encoder/Decoder & Password Hash Generator
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Sources/SwiftCryptoTools
Tests
.gitattributes
.gitignore
LICENSE
Package.resolved
Package.swift
README.md

README.md

macOS iOS Linux license

Swift Crypto Tools

Simple Swift Token Encoder/Decoder & Password Hash Generator

Implementation of JWT - JSON Web Token - alternative used for authentication. Use Token generator to store data from dictionary [String: Any] in encrypted token that can be safely passed using standard HTTP request methods to/from server and only decoded using known secret key.

When encrypted token is used, keeping track of open sessions on server is obsolete.

  • Example usage: If a user is authenticated using username/password combo, server API will store it's username and possibly token validity period inside encrypted token, send it to a user as a response, and later decode it's data whenever user send a request with token included.

Prerequisites

Swift

  • Open Source Swift 4.0.0 or higher

macOS

  • macOS Sierra 10.12.6 or higher
  • Xcode Version 9.0 (9A325) or higher

iOS

  • iOS 10.0 or higher

Linux

  • Ubuntu 16.04 & 16.10 (only tested on 16.04)

Getting started

import SwiftCryptoTools

API

Public variables & default values

The following public variables should be set to your desired values.

public var SCT_keyDerivationNumberOfRounds: UInt32 = 100000
public var SCT_derivationKeyLength: UInt = 64
public var SCT_initVectorSecretKey: String = "MySuperSecretKey"
public var SCT_defaultSaltString: String = "DefaultSalt"

Token struct

public struct Token: Codable {
    let token: String
}

generateToken()

Generate Token function takes two parameters (dictionary and secret key) and returns optional Token.

Implementation

// parameters to be passed to encoder
let secretKey: String = "YourSecretKey"
let dataCollection: [String: Any] = [
    "key_1": "value_1",
    "key_2": 2,
    "key_n": "value_n"
]

let token = generateToken(fromCollection: dataCollection, usingSecretKey: secretKey)

decodeToken()

Decoding Token function takes generated token and secret key and returns optional - initial [String: Any] collection.

Implementation

let initialCollection = decodeToken(fromToken: token, usingSecretKey: secretKey)

keyDerivation()

Key derivation encription function is used to generate hex string from hashed password/key using multiple parameters and .sha512 secure hash algorithm.

Implementation

// definition:
public func keyDerivation(from password: String, salt: String = SCT_defaultSaltString, rounds: UInt32 = SCT_keyDerivationNumberOfRounds, length: UInt = SCT_derivationKeyLength)

// usage:
let password = "PasswordString"
let hashedKey = keyDerivation(from: password)

Utilities

getHexValue()

String & Data extensions to represent themselves as hexadecimal string

Implementation

let hexValue: String = data_or_string.getHexValue()

License

MIT Licence