Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #16 from nodes-vapor/develop
Browse files Browse the repository at this point in the history
Develop -> Master
  • Loading branch information
Casperhr authored Mar 7, 2017
2 parents 4fe37e1 + 35ecc46 commit 039cb37
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Sources/String+Random.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Random

extension String {


/// Random alpha numeric string
///
/// Note this is not
///
/// - Parameter length: <#length description#>
/// - Returns: <#return value description#>
public static func randomAlphaNumericString(_ length: Int = 64) -> String {
let letters: String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let len = letters.count

var randomString = ""
for _ in 0 ..< length {
let rand = Int.random(min: 0, max: Int(len - 1))

let char = letters.substring(from: rand, length: 1)

randomString += char
}

return randomString
}

/// Random string
///
/// Note this string can use all chars, cannot be used for urls params fx
///
/// - Parameter length: length Int
/// - Returns: String
/// - Throws: Error
public static func random(_ length: Int = 64) throws -> String {
return try CryptoRandom.bytes(count: length).string()
}
}

0 comments on commit 039cb37

Please sign in to comment.