You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

76 lines
2.3 KiB

  1. import Foundation
  2. enum HMACAlgorithm {
  3. case MD5, SHA1, SHA224, SHA256, SHA384, SHA512
  4. func toCCHmacAlgorithm() -> CCHmacAlgorithm {
  5. var result: Int = 0
  6. switch self {
  7. case .MD5:
  8. result = kCCHmacAlgMD5
  9. case .SHA1:
  10. result = kCCHmacAlgSHA1
  11. case .SHA224:
  12. result = kCCHmacAlgSHA224
  13. case .SHA256:
  14. result = kCCHmacAlgSHA256
  15. case .SHA384:
  16. result = kCCHmacAlgSHA384
  17. case .SHA512:
  18. result = kCCHmacAlgSHA512
  19. }
  20. return CCHmacAlgorithm(result)
  21. }
  22. func digestLength() -> Int {
  23. var result: CInt = 0
  24. switch self {
  25. case .MD5:
  26. result = CC_MD5_DIGEST_LENGTH
  27. case .SHA1:
  28. result = CC_SHA1_DIGEST_LENGTH
  29. case .SHA224:
  30. result = CC_SHA224_DIGEST_LENGTH
  31. case .SHA256:
  32. result = CC_SHA256_DIGEST_LENGTH
  33. case .SHA384:
  34. result = CC_SHA384_DIGEST_LENGTH
  35. case .SHA512:
  36. result = CC_SHA512_DIGEST_LENGTH
  37. }
  38. return Int(result)
  39. }
  40. }
  41. extension String {
  42. func hmac(algorithm: HMACAlgorithm, key: String) -> String {
  43. let cKey = key.cString(using: String.Encoding.utf8)
  44. let cData = self.cString(using: String.Encoding.utf8)
  45. var result = [CUnsignedChar](repeating: 0, count: Int(algorithm.digestLength()))
  46. CCHmac(algorithm.toCCHmacAlgorithm(), cKey!, Int(strlen(cKey!)), cData!, Int(strlen(cData!)), &result)
  47. let hmacData:NSData = NSData(bytes: result, length: (Int(algorithm.digestLength())))
  48. var bytes = [UInt8](repeating:0, count: hmacData.length)
  49. hmacData.getBytes(&bytes, length: hmacData.length)
  50. var hexString = ""
  51. for byte in bytes {
  52. hexString += String(format:"%02hhx", UInt8(byte))
  53. }
  54. return hexString
  55. }
  56. }
  57. @objc(LessPassModule)
  58. class LessPassModule: NSObject {
  59. @objc(createFingerprint:resolver:rejecter:)
  60. func createFingerprint(_ masterPassword: String,
  61. resolver resolve: RCTPromiseResolveBlock,
  62. rejecter reject:RCTPromiseRejectBlock) -> Void {
  63. resolve("".hmac(algorithm: HMACAlgorithm.SHA256, key: masterPassword))
  64. }
  65. @objc
  66. static func requiresMainQueueSetup() -> Bool {
  67. return false
  68. }
  69. }