Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added canonical cbor map key sorting #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion Sources/SwiftCBOR/CBOREncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,55 @@ extension CBOR {
}

// MARK: - major 5: a map of pairs of data items

public static func encodeMap<A: CBOREncodable, B: CBOREncodable>(_ map: [A: B]) -> [UInt8] {
var res: [UInt8] = []
res.reserveCapacity(1 + map.count * (MemoryLayout<A>.size + MemoryLayout<B>.size + 2))
res = map.count.encode()
res[0] = res[0] | 0b101_00000

for (k, v) in map {
res.append(contentsOf: k.encode())
res.append(contentsOf: v.encode())
}
return res
}

// MARK: - major 5: a map of pairs of data items sorted canonically see: https://tools.ietf.org/html/rfc7049#section-3.9
public static func encodeMapCanonical<A: CBOREncodable, B: CBOREncodable>(_ map: [A: B]) -> [UInt8] {
var res: [UInt8] = []
res.reserveCapacity(1 + map.count * (MemoryLayout<A>.size + MemoryLayout<B>.size + 2))
res = map.count.encode()
res[0] = res[0] | 0b101_00000

let canonicalSortedEntries = map
.map { k, v in (k.encode(), v.encode()) }
.sorted { a, b in canonicalKeyOrder(a: a.0, b: b.0) }

for (k, v) in canonicalSortedEntries {
res.append(contentsOf: k)
res.append(contentsOf: v)
}
return res
}


// MARK: sort keys canonically, see: https://tools.ietf.org/html/rfc7049#section-3.9
private static func canonicalKeyOrder(a: [UInt8], b: [UInt8]) -> Bool {
if a.count == b.count {
for (index, byteA) in a.enumerated() {
let byteB = b[index]
if byteA == byteB {
continue
} else {
return byteA < byteB
}
}
return false
} else {
return a.count < b.count
}
}

public static func encodeMap<A: CBOREncodable>(_ map: [A: Any?]) throws -> [UInt8] {
var res: [UInt8] = []
res = map.count.encode()
Expand Down
6 changes: 6 additions & 0 deletions Tests/SwiftCBORTests/CBOREncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ class CBOREncoderTests: XCTestCase {
XCTAssertEqual(encodedMapToAny, [0xa2, 0x61, 0x61, 0x01, 0x61, 0x62, 0x82, 0x02, 0x03])
}

func testCanonicallyEncodeMaps() {
XCTAssertEqual(CBOR.encode(Dictionary<Int, Int>()), [0xa0])
let encoded = CBOR.encodeMapCanonical([1: 2, 3: 4])
XCTAssert(encoded == [0xa2, 0x01, 0x02, 0x03, 0x04])
}

func testEncodeTagged() {
let bignum: [UInt8] = [0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] // 2**64
let bignumCBOR = CBOR.byteString(bignum)
Expand Down