Skip to content

Commit

Permalink
Fix hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Oct 31, 2024
1 parent c86ee9b commit 99ba876
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
10 changes: 4 additions & 6 deletions Sources/Orders/OrdersServiceCustom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -391,18 +391,16 @@ extension OrdersServiceCustom {

// MARK: - order file generation
extension OrdersServiceCustom {
private static func generateManifestFile(
using encoder: JSONEncoder, in root: URL
) throws -> Data {
private static func generateManifestFile(using encoder: JSONEncoder, in root: URL) throws -> Data {
var manifest: [String: String] = [:]
let paths = try FileManager.default.subpathsOfDirectory(atPath: root.path)
for relativePath in paths {
let file = URL(fileURLWithPath: relativePath, relativeTo: root)
guard !file.hasDirectoryPath else { continue }
let data = try Data(contentsOf: file)
let hash = SHA256.hash(data: data)
manifest[relativePath] = hash.map { "0\(String($0, radix: 16))".suffix(2) }.joined()
manifest[relativePath] = try SHA256.hash(data: Data(contentsOf: file)).hex
}
// Write the manifest file to the root directory
// and return the data for using it in signing.
let data = try encoder.encode(manifest)
try data.write(to: root.appendingPathComponent("manifest.json"))
return data
Expand Down
6 changes: 3 additions & 3 deletions Sources/Passes/PassesServiceCustom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,10 @@ extension PassesServiceCustom {
for relativePath in paths {
let file = URL(fileURLWithPath: relativePath, relativeTo: root)
guard !file.hasDirectoryPath else { continue }
let data = try Data(contentsOf: file)
let hash = Insecure.SHA1.hash(data: data)
manifest[relativePath] = hash.map { "0\(String($0, radix: 16))".suffix(2) }.joined()
manifest[relativePath] = try Insecure.SHA1.hash(data: Data(contentsOf: file)).hex
}
// Write the manifest file to the root directory
// and return the data for using it in signing.
let data = try encoder.encode(manifest)
try data.write(to: root.appendingPathComponent("manifest.json"))
return data
Expand Down
2 changes: 1 addition & 1 deletion Tests/OrdersTests/OrdersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct OrdersTests {
let manifestJSONData = try String(contentsOfFile: orderFolder.path.appending("/manifest.json")).data(using: .utf8)
let manifestJSON = try decoder.decode([String: String].self, from: manifestJSONData!)
let iconData = try Data(contentsOf: orderFolder.appendingPathComponent("/icon.png"))
let iconHash = Array(SHA256.hash(data: iconData)).hex
let iconHash = SHA256.hash(data: iconData).hex
#expect(manifestJSON["icon.png"] == iconHash)
#expect(manifestJSON["pet_store_logo.png"] != nil)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/PassesTests/PassesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct PassesTests {
let manifestJSONData = try String(contentsOfFile: passFolder.path.appending("/manifest.json")).data(using: .utf8)
let manifestJSON = try decoder.decode([String: String].self, from: manifestJSONData!)
let iconData = try Data(contentsOf: passFolder.appendingPathComponent("/icon.png"))
let iconHash = Array(Insecure.SHA1.hash(data: iconData)).hex
let iconHash = Insecure.SHA1.hash(data: iconData).hex
#expect(manifestJSON["icon.png"] == iconHash)
#expect(manifestJSON["logo.png"] != nil)
#expect(manifestJSON["personalizationLogo.png"] != nil)
Expand Down Expand Up @@ -93,7 +93,7 @@ struct PassesTests {
let manifestJSONData = try String(contentsOfFile: passFolder.path.appending("/manifest.json")).data(using: .utf8)
let manifestJSON = try decoder.decode([String: String].self, from: manifestJSONData!)
let iconData = try Data(contentsOf: passFolder.appendingPathComponent("/personalizationLogo.png"))
let iconHash = Array(Insecure.SHA1.hash(data: iconData)).hex
let iconHash = Insecure.SHA1.hash(data: iconData).hex
#expect(manifestJSON["personalizationLogo.png"] == iconHash)
}
}
Expand Down

0 comments on commit 99ba876

Please sign in to comment.