Argon2Swift
public class Argon2Swift
Main class to handle all Argon2 hashing and verification
Usage Example
- Hash the password: “Password12”
let hashResult = try! Argon2Swift.hashPasswordString(password: "Password12", salt: Salt.newSalt())
let hashData = hashResult.hashData()
let base64Hash = hashResult.base64String()
let hexHash = hashResult.hexString()
let encodedData = hashResult.encodedData()
let encodedString = hashResult.encodedString()
-
Hashes a given
String
password with Argon2 utilizing the given salt as well as optionally the specific parameters of the hashing operation itselfThrows
Argon2SwiftException
if the hashing fails in any mannerDeclaration
Swift
public static func hashPasswordString(password: String, salt: Salt, iterations: Int = 32, memory: Int = 256, parallelism: Int = 2, length: Int = 32, type: Argon2Type = .i, version: Argon2Version = .V13) throws -> Argon2SwiftResult
Parameters
password
The
String
password to hash (utf-8 encoded)salt
The
Salt
to use with Argon2 as the salt in the hashing operationiterations
The amount of iterations that the algorithm should perform (optional parameter, defaults to 32)
memory
The amount of memory the hashing operation can use at a maximum (optional parameter, defaults to 256)
parallelism
The factor of parallelism when comouting the hash (optional parameter, defaults to 2)
length
The length of the final hash (optional parameter, defaults to 32)
type
The specific type of Argon2 to use, either
i
,d
, orid
(optional parameter, defaults to.i
)version
The version of Argon2 to use in the hashing computation, either
V10
orV13
(optional parameter, defaults to.V13
)Return Value
An
Argon2SwiftResult
containing the hash, encoding, and convenience methods to access the hash and encoded results in various forms -
Hashes a given
Data
password with Argon2 utilizing the given salt as well as optionally the specific parameters of the hashing operation itselfThrows
Argon2SwiftException
if the hashing fails in any mannerDeclaration
Swift
public static func hashPasswordBytes(password: Data, salt: Salt, iterations: Int = 32, memory: Int = 256, parallelism: Int = 2, length: Int = 32, type: Argon2Type = .i, version: Argon2Version = .V13) throws -> Argon2SwiftResult
Parameters
password
The
Data
password to hashsalt
The
Salt
to use with Argon2 as the salt in the hashing operationiterations
The amount of iterations that the algorithm should perform (optional parameter, defaults to 32)
memory
The amount of memory the hashing operation can use at a maximum (optional parameter, defaults to 256)
parallelism
The factor of parallelism when comouting the hash (optional parameter, defaults to 2)
length
The length of the final hash (optional parameter, defaults to 32)
type
The specific type of Argon2 to use, either
i
,d
, orid
(optional parameter, defaults to.i
)version
The version of Argon2 to use in the hashing computation, either
V10
orV13
(optional parameter, defaults to.V13
)Return Value
An
Argon2SwiftResult
containing the hash, encoding, and convenience methods to access the hash and encoded results in various forms -
Verifies a
String
password with the given encodedString
, returningtrue
on successful verifications andfalse
on incorrect ones.Throws
Argon2SwiftException
if the verification fails in any mannerDeclaration
Swift
public static func verifyHashString(password: String, hash: String, type: Argon2Type = .i) throws -> Bool
Parameters
password
The
String
password to verify and check (utf-8 encoded)encoded
The
String
encoded Argon2 value to check the password against (utf-8 encoded)type
The specific type of Argon2 to use, either
i
,d
, orid
(optional parameter, defaults to.i
)Return Value
A
Bool
signifying whether the password is equivalent to the hash or not -
Verifies a
Data
password with the given encodedData
, returningtrue
on successful verifications andfalse
on incorrect ones.Throws
Argon2SwiftException
if the verification fails in any mannerDeclaration
Swift
public static func verifyHashBytes(password: Data, hash: Data, type: Argon2Type = .i) throws -> Bool
Parameters
password
The
Data
password to verify and checkencoded
The
Data
encoded Argon2 value to check the password againsttype
The specific type of Argon2 to use, either
i
,d
, orid
(optional parameter, defaults to.i
)Return Value
A
Bool
signifying whether the password is equivalent to the hash or not -
A method to map the
Argon2Type
Swift enum to theArgon2_type
C struct in the reference libraryDeclaration
Swift
static func getArgon2Type(type: Argon2Type) -> Argon2_type
Parameters
type
The
Argon2Type
to get the equivalentArgon2_type
forReturn Value
An
Argon2_type
object to use in the C argon2 methods -
A method to set an
[Int8]
mutable pointer that the C methods will modify with resultsDeclaration
Swift
static func setPtr(length: Int) -> UnsafeMutablePointer<Int8>
Parameters
length
The length of the pointer to allocate
Return Value
An allocated
UnsafeMutablePointer<Int8>
with the given length -
A method to deinitialize and deallocate an
Int8
mutable pointer that has been already allocatedDeclaration
Swift
static func freePtr(pointer: UnsafeMutablePointer<Int8>, length: Int)
Parameters
pointer
The
UnsafeMutablePointer<Int8>
to deinitialize and freelength
The length of the given pointer