Proyectos de Subversion Iphone Microlearning - Inconcert

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
//
2
//  HeaderSecurity.swift
3
//  twogetskills
4
//
5
//  Created by Efrain Yanez Recanatini on 5/6/22.
6
//
7
 
8
import Foundation
9
import var CommonCrypto.CC_MD5_DIGEST_LENGTH
10
import func CommonCrypto.CC_MD5
11
import typealias CommonCrypto.CC_LONG
12
 
13
class HeaderSecurity
14
{
15
    public let created : Int
16
    public let rand : Int
17
    public var secret : String = ""
18
    private var appData = AppData.sharedInstance
19
 
20
 
21
    init()
22
    {
23
 
24
        created = Int(round(Date().timeIntervalSince1970))
25
        rand = Int.random(in: 0..<8999) + 1000
26
 
27
        let md5Data = MD5(string: "\(appData.devicePassword)" + ":" + "\(created)" + ":" + "\(rand)")
28
        secret =  md5Data.map {
29
            String(format: "%02hhx", $0)
30
 
31
        }.joined()
32
    }
33
 
34
 
35
    func MD5(string: String) -> Data {
36
        let length = Int(CC_MD5_DIGEST_LENGTH)
37
        let messageData = string.data(using:.utf8)!
38
        var digestData = Data(count: length)
39
 
40
        _ = digestData.withUnsafeMutableBytes { digestBytes -> UInt8 in
41
        messageData.withUnsafeBytes { messageBytes -> UInt8 in
42
        if let messageBytesBaseAddress = messageBytes.baseAddress, let digestBytesBlindMemory = digestBytes.bindMemory(to: UInt8.self).baseAddress {
43
                let messageLength = CC_LONG(messageData.count)
44
                    CC_MD5(messageBytesBaseAddress, messageLength, digestBytesBlindMemory)
45
                }
46
            return 0
47
            }
48
        }
49
        return digestData
50
    }
51
}