PKCS5.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. //public struct PKCS5 {
  13. //}
  14. //
  15. //extension PKCS5 {
  16. // //
  17. // // PBKDF2 - Password-Based Key Derivation Function 2. Key stretching technique.
  18. // // DK = PBKDF2(PRF, Password, Salt, c, dkLen)
  19. // //
  20. // struct PBKDF2 {
  21. // typealias Bytes = [UInt8]
  22. // private func calc(# hash:Hash, password:Bytes, salt:Bytes, c:Int, dkLen:Int) -> [UInt8]? {
  23. // if (dkLen > Int(pow(2,32) as Float - 1)) {
  24. // println("ERROR: derived key too long");
  25. // return nil
  26. // }
  27. //
  28. // if let prf = HMAC(password, variant: .sha256) { //FIXME: hardcoded SHA256
  29. // let hLen = prf.variant.size
  30. // let numBlocks = Int(ceilf(Float(dkLen) / Float(hLen))) // l
  31. // let lastBlockOctets = dkLen - (1 - numBlocks) * hLen // r
  32. // // blocks
  33. // for block in 1...numBlocks {
  34. // // for each block T_i = U_1 ^ U_2 ^ ... ^ U_iter
  35. // // U_1 = PRF(password, salt || uint(i))
  36. // var uinti = [UInt8](count: 4, repeatedValue: 0)
  37. // uinti[0] = UInt8(block >> 24)
  38. // uinti[1] = UInt8(block >> 16)
  39. // uinti[2] = UInt8(block >> 8)
  40. // uinti[3] = UInt8(block)
  41. // if let dk = prf.authenticate(message: salt + uinti) {
  42. // let T = dk[dk.count - hLen]
  43. // }
  44. // }
  45. // }
  46. // return nil
  47. // }
  48. // }
  49. //}