Proyectos de Subversion Iphone Microlearning

Rev

| 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
    let created : Int
16
    let rand : Int
17
    var secret : String = ""
18
 
19
    init()
20
    {
21
        let preference = Preference.sharedInstance
22
 
23
        created = Int(round(Date().timeIntervalSince1970))
24
        rand = Int.random(in: 0..<8999) + 1000
25
 
26
        let md5Data = MD5(string: "\(preference.password)" + ":" + "\(created)" + ":" + "\(rand)")
27
        secret =  md5Data.map {
28
            String(format: "%02hhx", $0)
29
 
30
        }.joined()
31
    }
32
 
33
 
34
    func MD5(string: String) -> Data {
35
        let length = Int(CC_MD5_DIGEST_LENGTH)
36
        let messageData = string.data(using:.utf8)!
37
        var digestData = Data(count: length)
38
 
39
        _ = digestData.withUnsafeMutableBytes { digestBytes -> UInt8 in
40
        messageData.withUnsafeBytes { messageBytes -> UInt8 in
41
        if let messageBytesBaseAddress = messageBytes.baseAddress, let digestBytesBlindMemory = digestBytes.bindMemory(to: UInt8.self).baseAddress {
42
                let messageLength = CC_LONG(messageData.count)
43
                    CC_MD5(messageBytesBaseAddress, messageLength, digestBytesBlindMemory)
44
                }
45
            return 0
46
            }
47
        }
48
        return digestData
49
    }
50
}