Added support to sign messages
This commit is contained in:
parent
0eb4c0069e
commit
84c1058e83
@ -10,8 +10,8 @@
|
||||
"vscode": {
|
||||
"extensions": [
|
||||
"sswg.swift-lang",
|
||||
"fabiospampinato.vscode-highlight",
|
||||
"fabiospampinato.vscode-todo-plus"
|
||||
//"fabiospampinato.vscode-highlight",
|
||||
//"fabiospampinato.vscode-todo-plus"
|
||||
]
|
||||
}
|
||||
},
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
/* public class EdgeDevice : EdgeDeviceP {
|
||||
public class EdgeDevice : EdgeDeviceP {
|
||||
|
||||
public let deviceID: String
|
||||
public let deviceType: DeviceType
|
||||
@ -56,8 +56,8 @@
|
||||
|
||||
// Todo: END Add Vincenzo's implementation
|
||||
return Message(
|
||||
msgType: MessageType,
|
||||
timestamp: Date,
|
||||
msgType: MessageType.Data,
|
||||
timestamp: ,
|
||||
deviceID: String,
|
||||
location: Location3D,
|
||||
fields: [Field],
|
||||
@ -70,4 +70,4 @@
|
||||
|
||||
|
||||
|
||||
} */
|
||||
}
|
||||
@ -1,26 +1,51 @@
|
||||
import Foundation
|
||||
import Crypto
|
||||
import Foundation
|
||||
|
||||
public class Message {
|
||||
|
||||
public let messageType : MessageType
|
||||
public let timestamp : Date
|
||||
public let deviceID : String
|
||||
public let location : Location3D
|
||||
public let fields : [Field]
|
||||
public let signature : [UInt8]
|
||||
|
||||
public let messageType: MessageType
|
||||
public let timestamp: Date
|
||||
public let deviceID: String
|
||||
public let location: Location3D
|
||||
public let fields: [Field]
|
||||
public var signature: String {
|
||||
get{
|
||||
return self._signature != nil ? self._signature! : "##INVALID"
|
||||
}
|
||||
}
|
||||
private var _signature: String? = nil
|
||||
|
||||
public init(
|
||||
msgType: MessageType,
|
||||
timestamp: Date,
|
||||
timestamp: Date,
|
||||
deviceID: String,
|
||||
location: Location3D,
|
||||
fields: [Field],
|
||||
signature: [UInt8]
|
||||
fields: [Field]
|
||||
) {
|
||||
self.messageType = msgType
|
||||
self.timestamp = timestamp
|
||||
self.deviceID = deviceID
|
||||
self.location = location
|
||||
self.fields = fields
|
||||
self.signature = signature
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public func toData() -> Data {
|
||||
var string: String = "" + "\(messageType)" + "\(timestamp)" + "\(deviceID)" + "\(location)"
|
||||
|
||||
for field in self.fields {
|
||||
string += "\(field)"
|
||||
}
|
||||
|
||||
return Data(string.utf8)
|
||||
}
|
||||
|
||||
public func signMessage(key: P521.Signing.PrivateKey) {
|
||||
do {
|
||||
self._signature = try sign(object: self.toData(), key: key)
|
||||
} catch {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
0
Sources/IoT-Simulator-Core/Protocols/Signable.swift
Normal file
0
Sources/IoT-Simulator-Core/Protocols/Signable.swift
Normal file
@ -1,13 +1,19 @@
|
||||
import Crypto // Equivalent to CryptoKit (more or less)
|
||||
import Foundation
|
||||
|
||||
|
||||
// ------------------
|
||||
// --- Sign ---------
|
||||
// ------------------
|
||||
|
||||
public func sign(string: String, key: P521.Signing.PrivateKey) throws -> String {
|
||||
let data = Data(string.utf8)
|
||||
return try key.signature<Data>(for: data).rawRepresentation
|
||||
return try sign(object: data, key: key)
|
||||
|
||||
}
|
||||
|
||||
public func sign(object: Data, key: P521.Signing.PrivateKey)throws -> String {
|
||||
return try key.signature<Data>(for: object).rawRepresentation.base64EncodedString()
|
||||
|
||||
}
|
||||
|
||||
@ -17,13 +23,13 @@ public func sign(string: String, key: P521.Signing.PrivateKey) throws -> String
|
||||
// --- Decrypt ------
|
||||
// ------------------
|
||||
|
||||
public func verify(signature: String, string: String, key: P521.Signing.PublicKey) throws -> Bool {
|
||||
public func verifySignature(signature: String, string: String, key: P521.Signing.PublicKey) throws -> Bool {
|
||||
let data = Data(string.utf8)
|
||||
|
||||
let ecdsa: P521.Signing.ECDSASignature
|
||||
do {
|
||||
let bytes = ecdsa
|
||||
ecdsa = try P521.Signing.ECDSASignature(rawRepresentation: signature)
|
||||
let bytes = Data(base64Encoded: signature)!
|
||||
ecdsa = try P521.Signing.ECDSASignature(rawRepresentation: bytes)
|
||||
} catch {
|
||||
throw SecurityError.NotDecodableError
|
||||
}
|
||||
@ -31,6 +37,19 @@ public func verify(signature: String, string: String, key: P521.Signing.PublicKe
|
||||
return key.isValidSignature<Data>(ecdsa, for: data)
|
||||
}
|
||||
|
||||
public func verifySignature(signature: String, object: Data, key: P521.Signing.PublicKey) throws -> Bool {
|
||||
|
||||
let ecdsa: P521.Signing.ECDSASignature
|
||||
do {
|
||||
let bytes = Data(base64Encoded: signature)!
|
||||
ecdsa = try P521.Signing.ECDSASignature(rawRepresentation: bytes)
|
||||
} catch {
|
||||
throw SecurityError.NotDecodableError
|
||||
}
|
||||
|
||||
return key.isValidSignature<Data>(ecdsa, for: object)
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ------------------
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import Testing
|
||||
import RandomCpp
|
||||
import Foundation
|
||||
import SwiftASN1
|
||||
import Crypto
|
||||
|
||||
@testable import IoT_Simulator_Core
|
||||
|
||||
@ -38,9 +38,10 @@ import SwiftASN1
|
||||
let keyPath = "./Private/privateKey.pem"
|
||||
let key = try pem2key(filePath: keyPath)
|
||||
|
||||
let obj = [1, 2, 3]
|
||||
let obj = "[1, 2, 3] "
|
||||
|
||||
let signature = try sign(object: obj, key: key)
|
||||
let signature = try sign(string: obj, key: key)
|
||||
print(signature)
|
||||
|
||||
|
||||
}
|
||||
@ -49,10 +50,14 @@ import SwiftASN1
|
||||
let keyPath = "./Private/privateKey.pem"
|
||||
let key = try pem2key(filePath: keyPath)
|
||||
|
||||
let obj = "[1, 2, 3]"
|
||||
let obj = "[1, 2, 3] "
|
||||
|
||||
let signature = try sign(object: obj, key: key)
|
||||
let signature = try sign(string: obj, key: key)
|
||||
let puKey = key.publicKey
|
||||
|
||||
|
||||
let verify = try verify(signature: signature, string: obj, key: puKey)
|
||||
print(verify)
|
||||
assert(verify)
|
||||
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user