PKCS5.swift 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // PKCS.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 12/03/15.
  6. // Copyright (c) 2015 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. // PKCS is a group of public-key cryptography standards devised
  9. // and published by RSA Security Inc, starting in the early 1990s.
  10. //
  11. // PKCS#5 http://tools.ietf.org/html/rfc2898
  12. import Foundation
  13. //public struct PKCS5 {
  14. //}
  15. //
  16. //extension PKCS5 {
  17. // //
  18. // // PBKDF2 - Password-Based Key Derivation Function 2. Key stretching technique.
  19. // // DK = PBKDF2(PRF, Password, Salt, c, dkLen)
  20. // //
  21. // struct PBKDF2 {
  22. // typealias Bytes = [UInt8]
  23. // private func calc(# hash:Hash, password:Bytes, salt:Bytes, c:Int, dkLen:Int) -> [UInt8]? {
  24. // if (dkLen > Int(pow(2,32) as Float - 1)) {
  25. // println("ERROR: derived key too long");
  26. // return nil
  27. // }
  28. //
  29. // if let prf = HMAC(password, variant: .sha256) { //FIXME: hardcoded SHA256
  30. // let hLen = prf.variant.size
  31. // let numBlocks = Int(ceilf(Float(dkLen) / Float(hLen))) // l
  32. // let lastBlockOctets = dkLen - (1 - numBlocks) * hLen // r
  33. // // blocks
  34. // for block in 1...numBlocks {
  35. // // for each block T_i = U_1 ^ U_2 ^ ... ^ U_iter
  36. // // U_1 = PRF(password, salt || uint(i))
  37. // var uinti = [UInt8](count: 4, repeatedValue: 0)
  38. // uinti[0] = UInt8(block >> 24)
  39. // uinti[1] = UInt8(block >> 16)
  40. // uinti[2] = UInt8(block >> 8)
  41. // uinti[3] = UInt8(block)
  42. // if let dk = prf.authenticate(message: salt + uinti) {
  43. // let T = dk[dk.count - hLen]
  44. // }
  45. // }
  46. // }
  47. // return nil
  48. // }
  49. // }
  50. //}