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