Remove CryptoKit in favour of Crypto
This commit is contained in:
parent
1c6c29d585
commit
562c6fb9c1
33
Sources/App/Message+Extensions.swift
Normal file
33
Sources/App/Message+Extensions.swift
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import Foundation
|
||||||
|
import Crypto
|
||||||
|
|
||||||
|
extension Message {
|
||||||
|
|
||||||
|
static var length: Int {
|
||||||
|
SHA256.byteCount + Content.length
|
||||||
|
}
|
||||||
|
|
||||||
|
init<T: Sequence>(decodeFrom data: T) where T.Element == UInt8 {
|
||||||
|
let count = SHA256.byteCount
|
||||||
|
self.mac = Data(data.prefix(count))
|
||||||
|
self.content = .init(decodeFrom: Array(data.dropFirst(count)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func isValid(using key: SymmetricKey) -> Bool {
|
||||||
|
HMAC<SHA256>.isValidAuthenticationCode(mac, authenticating: content.encoded, using: key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Message.Content {
|
||||||
|
|
||||||
|
func authenticate(using key: SymmetricKey) -> Message {
|
||||||
|
let mac = HMAC<SHA256>.authenticationCode(for: encoded, using: key)
|
||||||
|
return .init(mac: Data(mac.map { $0 }), content: self)
|
||||||
|
}
|
||||||
|
|
||||||
|
func authenticateAndSerialize(using key: SymmetricKey) -> Data {
|
||||||
|
let encoded = self.encoded
|
||||||
|
let mac = HMAC<SHA256>.authenticationCode(for: encoded, using: key)
|
||||||
|
return Data(mac.map { $0 }) + encoded
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,8 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import CryptoKit
|
|
||||||
import NIOCore
|
import NIOCore
|
||||||
|
|
||||||
struct Message: Equatable, Hashable {
|
struct Message: Equatable, Hashable {
|
||||||
|
|
||||||
static var length: Int {
|
|
||||||
SHA256Digest.byteCount + Content.length
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Content: Equatable, Hashable {
|
struct Content: Equatable, Hashable {
|
||||||
|
|
||||||
let time: UInt32
|
let time: UInt32
|
||||||
@ -28,17 +23,6 @@ struct Message: Equatable, Hashable {
|
|||||||
MemoryLayout<UInt32>.size * 2
|
MemoryLayout<UInt32>.size * 2
|
||||||
}
|
}
|
||||||
|
|
||||||
func authenticate(using key: SymmetricKey) -> Message {
|
|
||||||
let mac = HMAC<SHA256>.authenticationCode(for: encoded, using: key)
|
|
||||||
return .init(mac: Data(mac.map { $0 }), content: self)
|
|
||||||
}
|
|
||||||
|
|
||||||
func authenticateAndSerialize(using key: SymmetricKey) -> Data {
|
|
||||||
let encoded = self.encoded
|
|
||||||
let mac = HMAC<SHA256>.authenticationCode(for: encoded, using: key)
|
|
||||||
return Data(mac.map { $0 }) + encoded
|
|
||||||
}
|
|
||||||
|
|
||||||
var encoded: Data {
|
var encoded: Data {
|
||||||
time.encoded + id.encoded
|
time.encoded + id.encoded
|
||||||
}
|
}
|
||||||
@ -64,16 +48,6 @@ struct Message: Equatable, Hashable {
|
|||||||
self.init(decodeFrom: data)
|
self.init(decodeFrom: data)
|
||||||
}
|
}
|
||||||
|
|
||||||
private init<T: Sequence>(decodeFrom data: T) where T.Element == UInt8 {
|
|
||||||
let count = SHA256Digest.byteCount
|
|
||||||
self.mac = Data(data.prefix(count))
|
|
||||||
self.content = .init(decodeFrom: Array(data.dropFirst(count)))
|
|
||||||
}
|
|
||||||
|
|
||||||
func isValid(using key: SymmetricKey) -> Bool {
|
|
||||||
HMAC<SHA256>.isValidAuthenticationCode(mac, authenticating: content.encoded, using: key)
|
|
||||||
}
|
|
||||||
|
|
||||||
var encoded: Data {
|
var encoded: Data {
|
||||||
mac + content.encoded
|
mac + content.encoded
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user