Refactored utility functions to make them more readable and removed separate register endpoint.
This commit is contained in:
@@ -21,7 +21,7 @@ proc encrypt*(key: Key, iv: Iv, data: seq[byte], sequenceNumber: uint32): (seq[b
|
||||
var tag: AuthenticationTag
|
||||
|
||||
var ctx: GCM[aes256]
|
||||
ctx.init(key, iv, sequenceNumber.toBytes())
|
||||
ctx.init(key, iv, uint32.toBytes(sequenceNumber))
|
||||
|
||||
ctx.encrypt(data, encData)
|
||||
ctx.getTag(tag)
|
||||
@@ -36,7 +36,7 @@ proc decrypt*(key: Key, iv: Iv, encData: seq[byte], sequenceNumber: uint32): (se
|
||||
var tag: AuthenticationTag
|
||||
|
||||
var ctx: GCM[aes256]
|
||||
ctx.init(key, iv, sequenceNumber.toBytes())
|
||||
ctx.init(key, iv, uint32.toBytes(sequenceNumber))
|
||||
|
||||
ctx.decrypt(encData, data)
|
||||
ctx.getTag(tag)
|
||||
@@ -114,7 +114,7 @@ proc deriveSessionKey*(keyPair: KeyPair, publicKey: Key): Key =
|
||||
|
||||
# Add combined public keys to hash
|
||||
let combinedKeys: Key = combineKeys(keyPair.publicKey, publicKey)
|
||||
let hashMessage: seq[byte] = "CONQUEST".toBytes() & @combinedKeys
|
||||
let hashMessage: seq[byte] = string.toBytes("CONQUEST") & @combinedKeys
|
||||
|
||||
# Calculate Blake2b hash and extract the first 32 bytes for the AES key (https://monocypher.org/manual/blake2b)
|
||||
let hash = blake2b(hashMessage, sharedSecret)
|
||||
|
||||
@@ -4,7 +4,7 @@ type
|
||||
Packer* = ref object
|
||||
stream: StringStream
|
||||
|
||||
proc initPacker*(): Packer =
|
||||
proc init*(T: type Packer): Packer =
|
||||
result = new Packer
|
||||
result.stream = newStringStream()
|
||||
|
||||
@@ -64,7 +64,7 @@ type
|
||||
stream: StringStream
|
||||
position: int
|
||||
|
||||
proc initUnpacker*(data: string): Unpacker =
|
||||
proc init*(T: type Unpacker, data: string): Unpacker =
|
||||
result = new Unpacker
|
||||
result.stream = newStringStream(data)
|
||||
result.position = 0
|
||||
@@ -156,7 +156,7 @@ proc getVarLengthMetadata*(unpacker: Unpacker): string =
|
||||
return ""
|
||||
|
||||
# Read content
|
||||
return unpacker.getBytes(int(length)).toString()
|
||||
return Bytes.toString(unpacker.getBytes(int(length)))
|
||||
|
||||
# Serialization & Deserialization functions
|
||||
proc serializeHeader*(packer: Packer, header: Header, bodySize: uint32): seq[byte] =
|
||||
|
||||
@@ -13,7 +13,7 @@ const
|
||||
type
|
||||
PacketType* = enum
|
||||
MSG_TASK = 0'u8
|
||||
MSG_RESPONSE = 1'u8
|
||||
MSG_RESULT = 1'u8
|
||||
MSG_REGISTER = 2'u8
|
||||
MSG_HEARTBEAT = 100'u8
|
||||
|
||||
@@ -56,7 +56,8 @@ type
|
||||
RESULT_NO_OUTPUT = 2'u8
|
||||
|
||||
# Encryption
|
||||
type
|
||||
type
|
||||
Bytes* = seq[byte]
|
||||
Key* = array[32, byte]
|
||||
Iv* = array[12, byte]
|
||||
AuthenticationTag* = array[16, byte]
|
||||
|
||||
@@ -16,17 +16,17 @@ proc uuidToUint32*(uuid: string): uint32 =
|
||||
proc uuidToString*(uuid: uint32): string =
|
||||
return uuid.toHex(8)
|
||||
|
||||
proc toString*(data: seq[byte]): string =
|
||||
proc toString*(T: type Bytes, data: seq[byte]): string =
|
||||
result = newString(data.len)
|
||||
for i, b in data:
|
||||
result[i] = char(b)
|
||||
|
||||
proc toBytes*(data: string): seq[byte] =
|
||||
proc toBytes*(T: type string, data: string): seq[byte] =
|
||||
result = newSeq[byte](data.len)
|
||||
for i, c in data:
|
||||
result[i] = byte(c.ord)
|
||||
|
||||
proc toUint32*(data: seq[byte]): uint32 =
|
||||
proc toUint32*(T: type Bytes, data: seq[byte]): uint32 =
|
||||
if data.len != 4:
|
||||
raise newException(ValueError, "Expected 4 bytes for uint32")
|
||||
|
||||
@@ -44,13 +44,13 @@ proc toHexDump*(data: seq[byte]): string =
|
||||
else:
|
||||
result.add(" ") # Regular space
|
||||
|
||||
proc toBytes*(value: uint16): seq[byte] =
|
||||
proc toBytes*(T: type uint16, value: uint16): seq[byte] =
|
||||
return @[
|
||||
byte(value and 0xFF),
|
||||
byte((value shr 8) and 0xFF)
|
||||
]
|
||||
|
||||
proc toBytes*(value: uint32): seq[byte] =
|
||||
proc toBytes*(T: type uint32, value: uint32): seq[byte] =
|
||||
return @[
|
||||
byte(value and 0xFF),
|
||||
byte((value shr 8) and 0xFF),
|
||||
@@ -58,7 +58,7 @@ proc toBytes*(value: uint32): seq[byte] =
|
||||
byte((value shr 24) and 0xFF)
|
||||
]
|
||||
|
||||
proc toBytes*(value: uint64): seq[byte] =
|
||||
proc toBytes*(T: type uint64, value: uint64): seq[byte] =
|
||||
return @[
|
||||
byte(value and 0xFF),
|
||||
byte((value shr 8) and 0xFF),
|
||||
|
||||
Reference in New Issue
Block a user