Guides

Passwords and encryption

Protect a whole database with a password, and hash or encrypt individual properties such as passwords and API keys.

XOD has two separate protections. A database password guards the whole database. Property crypto hashes or encrypts single values inside it.

A password-protected database

Pass the password as the second argument when you open it:

using var db = new XodContext("data/app.xod", "correct horse");

A new database is created with that password. Opening it later without the password, or with the wrong one, throws Xod.SecurityException. Passwords are 1 to 256 characters.

Adding, changing and removing the password

using var db = new XodContext("data/app.xod");

db.Secure("correct horse");                           // add a password
db.ChangePassword("correct horse", "battery staple"); // change it
db.Loose("battery staple");                           // remove it

Secure on a database that already has a password throws SecurityException; use ChangePassword. The command-line tool can do the same with xod secure and xod loose (using --password-env or --password-stdin).

Password changes are all or nothing, across a process crash too: every file is written under the new password beside the old one (<file>.xpwnew), a commit record (app.xod.xpwlog) marks the switch, and only then are files replaced. If a crash or another program holding a file interrupts replacement, the context’s next write or the next open finishes it (or discards it if the switch wasn’t committed yet).

A password change is refused:

  • with TransactionException inside a transaction;
  • with InvalidOperationException inside a BeforeAction or AfterAction handler;
  • with XodException while crash leftovers wait in the folder (close and reopen first);
  • with SecurityException when removing or changing the password would leave [Crypto(AES256)] values unreadable (see A key of the database’s own).

Keep the password out of your source code: read it from configuration or a secret store. XOD can’t recover a database whose password is lost.

Hashing a property

For values you only ever need to check, such as a user’s password, store a one-way hash. [Crypto(CryptoMethod.SHA256)] hashes the value when the row is written:

public class UserAccount
{
    public int Id { get; set; }

    [UniqueKey]
    public string Email { get; set; }

    [Crypto(CryptoMethod.SHA256)]
    public string PasswordHash { get; set; }
}

Insert the plain value; what comes back is the hash. Check a login with CryptoHelper.VerifyHash, which compares in constant time:

using Xod.Helpers;

db.Insert(new UserAccount { Email = "a@example.com", PasswordHash = "s3cret" });

var user = db.Find<UserAccount>(u => u.Email == "a@example.com");
bool ok = CryptoHelper.VerifyHash("s3cret", user.PasswordHash, CryptoMethod.SHA256);

Updating a row doesn’t hash an existing hash a second time. SHA512 is also available; MD5 and SHA1 exist for databases from XOD 1.x and shouldn’t be used for new data. VerifyHash also verifies XOD 1.x digests, and DatabaseOptions.Legacy1xDigests = true keeps 1.x digests intact when written elsewhere (see Moving from XOD 1.x).

Encrypting a property

For values you need back, such as an API key, use [Crypto(CryptoMethod.AES256)]. The value is encrypted with AES-256-GCM in the files and decrypted when you read it:

public class Integration
{
    public int Id { get; set; }

    [Crypto(CryptoMethod.AES256)]
    public string ApiKey { get; set; }
}

var row = db.Find<Integration>(i => i.Id == 1);
Console.WriteLine(row.ApiKey); // the plain value

If a stored value has been altered, reading it throws SecurityException rather than returning corrupted data.

A key of the database’s own

AES256 values are encrypted with a random 256-bit key generated for the database and stored in app.xod, protected by DatabaseOptions.PropertyEncryptionKey when provided or by the database password otherwise. PropertyEncryptionKey is a secret your application supplies and XOD never writes to disk.

var options = new DatabaseOptions
{
    PropertyEncryptionKey = Environment.GetEnvironmentVariable("APP_PROPERTY_KEY"),
};

using var db = new XodContext("data/app.xod", options: options);
db.Insert(new Integration { ApiKey = "example-api-key" });
  • An AES256 value requires a password or PropertyEncryptionKey. With neither, writing throws SecurityException.
  • Key creation: a new database opened with a password or PropertyEncryptionKey creates its key immediately (outside any transaction, so a rollback doesn’t undo it); a database created without either creates its key when it first gets a secret or first stores an AES256 value.
  • Values survive password changes: Secure, ChangePassword and Loose re-wrap the database’s data key without rewriting rows. Loose throws SecurityException while AES256 values rely on the database password unless PropertyEncryptionKey is supplied to protect the key instead.
  • Matching secret required: when PropertyEncryptionKey protects the key, opening without it (or with a different key) throws SecurityException when reading or writing an AES256 value.
  • Type and index rules: [Crypto] applies to string properties only (CryptoDataTypeException on other types), covers both elements and XML attributes, and can’t be combined with Indexed = true, [UniqueKey] or [PrimaryKey] (IndexDataTypeException).
  • CryptoHelper.EncryptProperty, CryptoHelper.DecryptProperty, XodSecurityService and IXodSecurityService are obsolete; use [Crypto(CryptoMethod.AES256)] and the context’s Secure, ChangePassword and Loose methods instead.

Source on GitHub · Report a problem with this page