Commit 63ae15b64d
63ae15b64d76b8b7c4d413cf6a6c5434c50d402d
parent: 79beff96cc
Verified · cmc
cmc <hello@cleberg.net> · 2026-11-26 17:57 UTC
Keep an unchanged entry's message after Decrypt Entry in File
Changed files (2)
Sources/OrgDocument/Crypt.swift
+3 −2 · view
| @@ -100,11 +100,12 @@ public final class CryptVault: @unchecked Sendable { |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | /// `passphrase` encrypts and decrypts `path` until it's forgotten. Messages kept for reuse |
| 103 | | /// under another passphrase are dropped. |
| 103 | /// under another passphrase are dropped; those recorded before any passphrase was known |
| 104 | /// were opened with this one and are kept. |
| 104 | 105 | public func unlock(_ passphrase: String, for path: String) { |
| 105 | 106 | let key = Self.key(path) |
| 106 | 107 | locked { |
| 107 | | if passphrases[key] != passphrase { |
| 108 | if let known = passphrases[key], known != passphrase { |
| 108 | 109 | reuse[key] = nil |
| 109 | 110 | lastFile[key] = nil |
| 110 | 111 | } |
Tests/OrgAppTests/CryptSaveTests.swift
+17 · view
| @@ -233,6 +233,23 @@ struct CryptSaveTests { |
| 233 | 233 | #expect((try String(contentsOf: other, encoding: .utf8)) == "x" + encrypted) |
| 234 | 234 | } |
| 235 | 235 | |
| 236 | @Test func decryptInFileKeepsTheMessageInTheSheetsOrder() throws { |
| 237 | let bank = (Self.file as NSString).range(of: "* Bank").location |
| 238 | let encrypted = try #require(try OrgCrypt.encrypt(Self.file, at: bank, passphrase: "pw")) |
| 239 | try notes.write("a.org", encrypted) |
| 240 | session.open(file.path) |
| 241 | // The passphrase sheet records the message before the passphrase is known to the session. |
| 242 | let message = try #require(OrgCrypt.firstMessage(encrypted)) |
| 243 | let decrypted = try OrgCrypt.decryptInPlace(encrypted, at: bank, passphrase: "pw") |
| 244 | let body = try #require(OrgCrypt.bodyRange(decrypted, at: bank)) |
| 245 | session.cryptDecrypted(message: message, plaintext: (decrypted as NSString).substring(with: NSRange(body)), for: file.path) |
| 246 | session.cryptUsed("pw", for: file.path) |
| 247 | try replace(with: decrypted) |
| 248 | try type("More. ", at: offset(of: "Open text.")) |
| 249 | #expect(session.save()) |
| 250 | #expect(disk == (encrypted as NSString).replacingOccurrences(of: "Open text.", with: "More. Open text.")) |
| 251 | } |
| 252 | |
| 236 | 253 | @Test func offWritesWhatTheBufferHas() throws { |
| 237 | 254 | session.encryptsTaggedEntries = false |
| 238 | 255 | try notes.write("a.org", "* Plain\n") |