V0.6.0 Arroyo Toad
Added the capability to sign and verify P521 Signature
This commit is contained in:
@@ -1,87 +1,120 @@
|
||||
import Foundation
|
||||
|
||||
import Testing
|
||||
@testable import MessageUtils
|
||||
|
||||
|
||||
|
||||
@Test func serializeMessage() async throws {
|
||||
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
|
||||
let msg = Message(
|
||||
version: 1,
|
||||
messageType: .KEEPALIVE,
|
||||
devType: .EDGE_SENSOR,
|
||||
RESERVED: 0,
|
||||
signType: .P521,
|
||||
timestamp: Date(),
|
||||
devID: 1,
|
||||
location: Location(x: 10,y: 20,z: 1),
|
||||
fields: [],
|
||||
signature: Array(repeating: 255, count: 132)
|
||||
)
|
||||
|
||||
let data = try serializeV1(msg: msg)
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test func serializeMessageWithField() async throws {
|
||||
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
|
||||
let msg = Message(
|
||||
version: 1,
|
||||
messageType: .KEEPALIVE,
|
||||
devType: .EDGE_SENSOR,
|
||||
RESERVED: 0,
|
||||
signType: .P521,
|
||||
timestamp: Date(),
|
||||
devID: 1,
|
||||
location: Location(x: 10,y: 20,z: 1),
|
||||
fields: [
|
||||
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8)),
|
||||
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8))
|
||||
],
|
||||
signature: Array(repeating: 255, count: 132)
|
||||
)
|
||||
|
||||
let data = try serializeV1(msg: msg)
|
||||
|
||||
}
|
||||
|
||||
@Test func serializeDeserializeMessage() async throws {
|
||||
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
|
||||
let msg = Message(
|
||||
version: 1,
|
||||
messageType: .KEEPALIVE,
|
||||
devType: .EDGE_SENSOR,
|
||||
RESERVED: 0,
|
||||
signType: .P521,
|
||||
timestamp: Date(),
|
||||
devID: 1,
|
||||
location: Location(x: 10,y: 20,z: 1),
|
||||
fields: [
|
||||
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8)),
|
||||
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8))
|
||||
],
|
||||
signature: Array(repeating: 255, count: 132)
|
||||
)
|
||||
|
||||
print(msg.toString())
|
||||
|
||||
let data = try serializeV1(msg: msg)
|
||||
|
||||
let bytes = data.map { value in
|
||||
return value
|
||||
}
|
||||
|
||||
|
||||
|
||||
print(bytes.count % 8 == 0)
|
||||
|
||||
|
||||
let msg2 = try derializeV1(serializedData: data)
|
||||
print(msg2.toString())
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
import Foundation
|
||||
|
||||
import Testing
|
||||
|
||||
@testable import MessageUtils
|
||||
|
||||
@Test func serializeMessage() async throws {
|
||||
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
|
||||
let msg = SignedMessage(
|
||||
version: 1,
|
||||
messageType: .KEEPALIVE,
|
||||
devType: .EDGE_SENSOR,
|
||||
RESERVED: 0,
|
||||
signType: .P521,
|
||||
timestamp: Date(),
|
||||
devID: 1,
|
||||
location: Location(x: 10, y: 20, z: 1),
|
||||
fields: [],
|
||||
signature: Array(repeating: 255, count: 132)
|
||||
)
|
||||
|
||||
let data = try serializeV1(msg: msg)
|
||||
|
||||
}
|
||||
@Test func serializeMessageWithField() async throws {
|
||||
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
|
||||
let msg = Message(
|
||||
version: 1,
|
||||
messageType: .KEEPALIVE,
|
||||
devType: .EDGE_SENSOR,
|
||||
RESERVED: 0,
|
||||
signType: .P521,
|
||||
timestamp: Date(),
|
||||
devID: 1,
|
||||
location: Location(x: 10, y: 20, z: 1),
|
||||
fields: [
|
||||
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8)),
|
||||
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8)),
|
||||
]
|
||||
)
|
||||
|
||||
let data = try serializeV1(msg: msg)
|
||||
|
||||
}
|
||||
@Test func serializeDeserializeMessage() async throws {
|
||||
let keyPath = "./Private/privateKey.pem"
|
||||
let key = try pem2key(filePath: keyPath)
|
||||
let publicKey = key.publicKey
|
||||
|
||||
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
|
||||
let msg = Message(
|
||||
version: 1,
|
||||
messageType: .KEEPALIVE,
|
||||
devType: .EDGE_SENSOR,
|
||||
RESERVED: 0,
|
||||
signType: .P521,
|
||||
timestamp: Date(),
|
||||
devID: 1,
|
||||
location: Location(x: 10, y: 20, z: 1),
|
||||
fields: [
|
||||
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8)),
|
||||
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8)),
|
||||
Field(key: Array("69".utf8), value: Array("Nice".utf8)),
|
||||
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8)),
|
||||
Field(key: Array("Live Love".utf8), value: Array("Laugh".utf8)),
|
||||
]
|
||||
)
|
||||
|
||||
print(msg.toString())
|
||||
|
||||
var data = serializeV1(msg: msg)
|
||||
|
||||
let bytes = data.map { value in
|
||||
return value
|
||||
}
|
||||
|
||||
let actualBytes = countBytes(msg: msg)
|
||||
|
||||
print("Number of Bytes for the message: \(bytes.count)")
|
||||
print("Number of Computed Bytes: \(actualBytes)")
|
||||
|
||||
let signature = try signMessage(msgData: data, signType: msg.signType, key: key)
|
||||
data.append(contentsOf: signature)
|
||||
|
||||
let msg2 = try deserializeV1(serializedData: data)
|
||||
let authenticMessage = try verifyMessageSignature(message: msg2, key: publicKey)
|
||||
|
||||
#expect(authenticMessage, "If this is not true, then we are doing some deserialization errors")
|
||||
print(msg2.toString())
|
||||
|
||||
}
|
||||
@Test func serializeMessageForLaterUse() async throws {
|
||||
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
|
||||
|
||||
let keyPath = "./Private/privateKey.pem"
|
||||
let key = try pem2key(filePath: keyPath)
|
||||
|
||||
let msg = Message(
|
||||
version: 1,
|
||||
messageType: .KEEPALIVE,
|
||||
devType: .EDGE_SENSOR,
|
||||
RESERVED: 0,
|
||||
signType: .P521,
|
||||
timestamp: Date(),
|
||||
devID: 1,
|
||||
location: Location(x: 10, y: 20, z: 1),
|
||||
fields: [
|
||||
Field(key: Array("valueOfLife".utf8), value: Array("42".utf8)),
|
||||
Field(key: Array("Live Love".utf8), value: Array("Laugh".utf8)),
|
||||
]
|
||||
)
|
||||
|
||||
var data = try serializeV1(msg: msg)
|
||||
|
||||
let signature = try signMessage(msgData: data, signType: msg.signType, key: key)
|
||||
data.append(Data(signature))
|
||||
|
||||
try data.write(to: URL(filePath: "./Private/signedMessage/Message.bin"))
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user