Random Password Generator Swift 3? - ios

I'm building a random password generator for iOS. In it, a button generates a random password that has characteristics chosen by the user (e.g. switches to turn on or off lowercase and uppercase letters, characters and symbols, and so on).
The UI looks great, the rest of the code is working smoothly, but I can't get my button to actually generate a random alphanumerical string. I have a label with some placeholder text ("Your Password") that should have its text updated to a random string when the button is pushed, but I am getting a compiler error: "unresolved use of identifier 'length'"
Here is the current code for the button:
#IBAction func generatePassword(_ sender: UIButton) {
let randomPasswordArray: NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
let len = UInt32(randomPasswordArray.length)
var randomPassword = ""
for _ in 0 ..< length {
let rand = arc4random(len)
var nextChar = randomPasswordArray.character(at: Int(rand))
randomPassword += NSString(characters: &nextChar, length: 1) as String
}
passwordLabel.text = "randomPassword"
}
Thanks!

First create an array with your password allowed characters
let passwordCharacters = Array("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".characters)
then choose the password lenght
let len = 8
define and empty string password
var password = ""
create a loop to gennerate your random characters
for _ in 0..<len {
// generate a random index based on your array of characters count
let rand = arc4random_uniform(UInt32(passwordCharacters.count))
// append the random character to your string
password.append(passwordCharacters[Int(rand)])
}
print(password) // "V3VPk5LE"
Swift 4
You can also use map instead of a standard loop:
let pswdChars = Array("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
let rndPswd = String((0..<len).map{ _ in pswdChars[Int(arc4random_uniform(UInt32(pswdChars.count)))]})
print(rndPswd) // "oLS1w3bK\n"
Swift 4.2
Using the new Collection's randomElement() method:
let len = 8
let pswdChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
let rndPswd = String((0..<len).compactMap{ _ in pswdChars.randomElement() })
print(rndPswd) // "3NRQHoiA\n"

You can also use
import Security
let password = SecCreateSharedWebCredentialPassword() as String?
print(password)

Your code should be
...
for _ in 0 ..< len {
...

Related

How to convert sequence of ASCII code into string in swift 4?

I have an sequence of ASCII codes in string format like (7297112112121326610511411610410097121). How to convert this into text format.
I tried below code :
func convertAscii(asciiStr: String) {
var asciiString = ""
for asciiChar in asciiStr {
if let number = UInt8(asciiChar, radix: 2) { // Cannot invoke initializer for type 'UInt8' with an argument list of type '(Character, radix: Int)'
print(number)
let character = String(describing: UnicodeScalar(number))
asciiString.append(character)
}
}
}
convertAscii(asciiStr: "7297112112121326610511411610410097121")
But getting error in if let number line.
As already mentioned decimal ASCII values are in range of 0-255 and can be more than 2 digits
Based on Sulthan's answer and assuming there are no characters < 32 (0x20) and > 199 (0xc7) in the text this approach checks the first character of the cropped string. If it's "1" the character is represented by 3 digits otherwise 2.
func convertAscii(asciiStr: String) {
var source = asciiStr
var result = ""
while source.count >= 2 {
let digitsPerCharacter = source.hasPrefix("1") ? 3 : 2
let charBytes = source.prefix(digitsPerCharacter)
source = String(source.dropFirst(digitsPerCharacter))
let number = Int(charBytes)!
let character = UnicodeScalar(number)!
result += String(character)
}
print(result) // "Happy Birthday"
}
convertAscii(asciiStr: "7297112112121326610511411610410097121")
If we consider the string to be composed of characters where every character is represented by 2 decimal letters, then something like this would work (this is just an example, not optimal).
func convertAscii(asciiStr: String) {
var source = asciiStr
var characters: [String] = []
let digitsPerCharacter = 2
while source.count >= digitsPerCharacter {
let charBytes = source.prefix(digitsPerCharacter)
source = String(source.dropFirst(digitsPerCharacter))
let number = Int(charBytes, radix: 10)!
let character = UnicodeScalar(number)!
characters.append(String(character))
}
let result: String = characters.joined()
print(result)
}
convertAscii(asciiStr: "7297112112121326610511411610410097121")
However, the format itself is ambigious because ASCII characters can take from 1 to 3 decimal digits, therefore to parse correctly, you need all characters to have the same length (e.g. 1 should be 001).
Note that I am taking always the same number of letters, then convert them to a number and then create a character the number.

Remove special characters from the string

I am trying to use an iOS app to dial a number. The problem is that the number is in the following format:
po placeAnnotation.mapItem.phoneNumber!
"‎+1 (832) 831-6486"
I want to get rid of some special characters and I want the following:
832-831-6486
I used the following code but it did not remove anything:
let charactersToRemove = CharacterSet(charactersIn: "()+-")
var telephone = placeAnnotation.mapItem.phoneNumber?.trimmingCharacters(in: charactersToRemove)
Any ideas?
placeAnnotation.mapItem.phoneNumber!.components(separatedBy: CharacterSet.decimalDigits.inverted)
.joined()
Here you go!
I tested and works well.
If you want something similar to CharacterSet with some flexibility, this should work:
let phoneNumber = "1 (832) 831-6486"
let charsToRemove: Set<Character> = Set("()+-".characters)
let newNumberCharacters = String(phoneNumber.characters.filter { !charsToRemove.contains($0) })
print(newNumberCharacters) //prints 1 832 8316486
I know the question is already answered, but to format phone numbers in any way one could use a custom formatter like below
class PhoneNumberFormatter:Formatter
{
var numberFormat:String = "(###) ### ####"
override func string(for obj: Any?) -> String? {
if let number = obj as? NSNumber
{
var input = number as Int64
var output = numberFormat
while output.characters.contains("#")
{
if let range = output.range(of: "#", options: .backwards)
{
output = output.replacingCharacters(in: range, with: "\(input % 10)")
input /= 10
}
else
{
output.replacingOccurrences(of: "#", with: "")
}
}
return output
}
return nil
}
func string(from number:NSNumber) -> String?
{
return string(for: number)
}
}
let phoneNumberFormatter = PhoneNumberFormatter()
//Digits will be filled backwards in place of hashes. It is easy change the custom formatter in anyway
phoneNumberFormatter.numberFormat = "###-##-##-##-##"
phoneNumberFormatter.string(from: 18063783889)
Swift 3
func removeSpecialCharsFromString(_ str: String) -> String {
struct Constants {
static let validChars = Set("1234567890-".characters)
}
return String(str.characters.filter { Constants.validChars.contains($0) })
}
To Use
let str : String = "+1 (832) 831-6486"
let newStr : String = self.removeSpecialCharsFromString(str)
print(newStr)
Note: you can add validChars which you want in string after operation perform.
If you have the number and special character in String format the use following code to remove special character
let numberWithSpecialChar = "1800-180-0000"
let actulNumber = numberWithSpecialChar.components(separatedBy: CharcterSet.decimalDigit.inverted).joined()
Otherwise, If you have the characters and special character in String format the use following code to remove special character
let charactersWithSpecialChar = "A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal-Panama!"
let actulString = charactersWithSpecialChar.components(separatedBy: CharacterSet.letters.inverted).joined(separator: " ")
NSString *str = #"(123)-456-7890";
NSLog(#"String: %#", str);
// Create character set with specified characters
NSMutableCharacterSet *characterSet =
[NSMutableCharacterSet characterSetWithCharactersInString:#"()-"];
// Build array of components using specified characters as separtors
NSArray *arrayOfComponents = [str componentsSeparatedByCharactersInSet:characterSet];
// Create string from the array components
NSString *strOutput = [arrayOfComponents componentsJoinedByString:#""];
NSLog(#"New string: %#", strOutput);

Realm Swift adding object with String property having character count > 1874 is crashing

I have a csv file with 200K rows.
each row contains 9 values, all the rows String character count is less than 2048 but 3 rows have character count equal to 4420. These rows indexes are 134481, 134482, 134483 respectively. Out of the 9 values in the row last string value is big.
When row 134481 is adding to realm or creating in realm, it get crashed at BpTree::create_root_from_mem with EXC_BAD_ACCESS(code=2, address=xxxxx)
Below is the swift code [swift version 2.2, xcode: 7.3, realmSwift: 1.1.0]...for adding the csv objects to realm database.
This code is running in a background queue. I tried skipping first 130k rows and even then it crashes exactly at row 134481. Only after reducing the string character count the object is added without crash.
class PackageObject:Object {
dynamic var id = ""
dynamic var packageBatch = ""
dynamic var packageCode = ""
dynamic var packageDescription = ""
dynamic var packageName = ""
dynamic var packagelocalName = ""
dynamic var packageNumber = ""
dynamic var packageBuild = ""
dynamic var packageSummary = ""
}
let filename = NSBundle.mainBundle().pathForResource("codes", ofType: "tsv")
if let realm = try? Realm(configuration: configuration), data = try? String.init(contentsOfFile: filename!) {
let block = 5000
var lineNumber = 0
print(realm.configuration.fileURL)
data.enumerateLines({ (line, stop) in
if lineNumber % block == 0 {
print("begin: ", lineNumber)
realm.beginWrite()
}
lineNumber += 1
var strings = line.componentsSeparatedByString("\t")
if lineNumber >= 134481 && lineNumber <= 134483 {
// strings[8] count is about 3805
// after reducing the count to 1874, the object
// is successfully added to the realm, else a crash is observed.
let count = strings[8].characters.count
print(count, line.characters.count)
let index = strings[8].endIndex.advancedBy(1931 - count)
strings[8].removeRange(index..<strings[8].endIndex)
}
let packageObj = PackageObject(value: strings)
realm.add(packageObj)
//realm.create(PackageObject.self, value:strings)
if lineNumber % block == 0 {
print("commit: ", lineNumber)
_ = try? realm.commitWrite()
}
})
}
// The last commitWrite is not handled...inside the block.
// for the below condition...
if lineNumber % block != 0 {
_ = try? realm.commitWrite()
}
The realm doc says a String property can be less than 16MB, but here its not even taking 10KB.
I don't find anything wrong with the string, it is just plain English text. Is this a bug or do I have to add objects in a different way.
If the string property is indexed, upgrading to 2.0 will probably fix this.
Older versions had a flaw in how the index was implemented that lead to it hitting a stack overflow when strings have sufficiently long common prefixes, and 2.0 changed how indexes are stored to remove the recursion.

swift2 decrypt MD5

Hello I just want to decrypt from md5 to 'normal string'
extension String {
func MD5() -> String {
var data = (self as NSString).dataUsingEncoding(NSUTF8StringEncoding)
let result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))
let resultBytes = UnsafeMutablePointer<CUnsignedChar>(result!.mutableBytes)
CC_MD5(data!.bytes, CC_LONG(data!.length), resultBytes)
let buff = UnsafeBufferPointer<CUnsignedChar>(start: resultBytes, count: result!.length)
let hash = NSMutableString()
for i in buff {
hash.appendFormat("%02x", i)
}
return hash as String
}
var x = "abc".MD5()
I want to get back to abc from "x"
It's not possible that's the whole point of hashing. You can however bruteforce by going through all possibilities (using all possible digits characters in every possible order) and hashing them and checking for a collision.
it was hard to reverse.
also check...https://en.wikipedia.org/wiki/MD5
Simple: Not possible, because MD5 hash is not possible to invert.
Check about One way function

Short Random Unique alphanumeric keys similar to Youtube IDs, in Swift

Is there a way to create random unique IDs similar to the YouTube IDs in Swift?
I know there are similar answers on this link, but they are for Php. But I want something in Swift.
I have tried using timestamp and UUIDs, but I want an alphanumeric short keys which would be around 4-10 characters so users can easily share with others verbally.
Thanks.
Looking for just a unique string
You can use UUIDs they're pretty cool:
let uuid = NSUUID().UUIDString
print(uuid)
From the  docs
UUIDs (Universally Unique Identifiers), also known as GUIDs (Globally
Unique Identifiers) or IIDs (Interface Identifiers), are 128-bit
values. UUIDs created by NSUUID conform to RFC 4122 version 4 and are
created with random bytes.
Some info about uuid: https://en.wikipedia.org/wiki/Universally_unique_identifier
Looking for a more specific length
Try something like this:
func randomStringWithLength(len: Int) -> NSString {
let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let randomString : NSMutableString = NSMutableString(capacity: len)
for _ in 1...len{
let length = UInt32 (letters.length)
let rand = arc4random_uniform(length)
randomString.appendFormat("%C", letters.character(at: Int(rand)))
}
return randomString
}
But i'll keep my answer incase someone else stumbles upon this looking for a UUID
This will allow you to create a random short code. It can create codes from Hexadecimal all the way to base 62 codes and of varying lengths.
aka.
let myCode = ShortCodeGenerator.getCode(length: 6)
3dH7t8,
fdE7j1,
Gl6jKd,
Hv8gU3,
let myBase32Code = ShortCodeGenerator.getCode(withBase: UInt32(32), length: 6)
3HF75J,
J67N9D,
B47SO3,
L9SD2N
You would have to check for redundancy and then create a new one if it has already been used.
struct ShortCodeGenerator {
private static let base62chars = [Character]("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".characters)
private static let maxBase : UInt32 = 62
static func getCode(withBase base: UInt32 = maxBase, length: Int) -> String {
var code = ""
for _ in 0..<length {
let random = Int(arc4random_uniform(min(base, maxBase)))
code.append(base62chars[random])
}
return code
}
}
This answer was useful in creating the above code and also has good
information on the number of unique identifiers you can have with each base number and length.
The total number of unique identifiers you need can be calculated by the equation:
BaseNumber^length = # of unique IDs
EDIT:
I have added even more functionality for converting Int's and NSDate's to shortcodes for my own project as well and put those into a project on GitHub.
Updated for swift 3:
If you want to generate Short Random Unique alphanumeric keys, used below lines of codes;
//function defination:
func generateTransactionId(length: Int) -> String {
var result = ""
let base62chars = [Character]("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".characters)
let maxBase : UInt32 = 62
let minBase : UInt16 = 32
for _ in 0..<length {
let random = Int(arc4random_uniform(UInt32(min(minBase, UInt16(maxBase)))))
result.append(base62chars[random])
}
return result
}
//function call:
let mTranactionId = self.generateTransactionId(length: 5) // you can change the value of length as you want
print("mTranactionId: \(mTranactionId)")
Ex: Result looks like: XHA7K, QTC92, MS1PT, YE2PV
//Enjoy coding...!
Does NSUUID().UUIDString do what you need?
I'm very pleased with NanoID.
https://github.com/ai/nanoid
...and here's the SWIFT version:
https://github.com/antiflasher/NanoID
You just need to drag 1 file (NanoID.swift) into your project, and you are good to go!
TAGS: Short GUID, Short UUID, Short Unique ID, Swift, iOS
Swift custom alternative(these version doesn't check for duplicate char):
func randomString(length: Int) -> String {
let letters = Array("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
var randomString: String = ""
for _ in 0..<length {
let randomNumber = Int.random(in: 0..<letters.count)
randomString.append(letters[randomNumber])
}
return randomString
}
You can archive this just using UUID.
If you don't want the whole string, as any String in Swift, you select a small portion of the string by using range, like this:
let text = UUID().uuidString
let index = text.index(text.startIndex, offsetBy: 8)
let small = text[text.startIndex..<index]
Notice 8 is the length of string I suggested, you can improve this by clamping this value using min(size, text.count) for example.
And finally, small is a Substring, to cast it to String just cast as usual - String(small)
Swift 5 version using a String extension
extension String {
static func random(length: Int) -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return String((0..<length).compactMap { _ in
letters.randomElement()
})
}
}

Resources