Pure Perl implementation of Mozilla SOPS (Secrets OPerationS) encrypted file format.
use File::SOPS;
# Encrypt a data structure
my $encrypted = File::SOPS->encrypt(
data => {
database => {
password => 'secret123',
host => 'db.example.com',
},
},
recipients => ['age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p'],
format => 'yaml', # or 'json'
);
# Decrypt
my $data = File::SOPS->decrypt(
encrypted => $encrypted,
identities => ['AGE-SECRET-KEY-1...'],
);
# File operations
File::SOPS->encrypt_file(
input => 'secrets.yaml',
output => 'secrets.enc.yaml',
recipients => ['age1...'],
);
File::SOPS->decrypt_file(
input => 'secrets.enc.yaml',
output => 'secrets.yaml',
identities => ['AGE-SECRET-KEY-1...'],
);
# Extract single value without decrypting entire file
my $password = File::SOPS->extract(
file => 'secrets.enc.yaml',
path => 'database.password',
identities => ['AGE-SECRET-KEY-1...'],
);
# Rotate data key (re-encrypt with new key)
File::SOPS->rotate(
file => 'secrets.enc.yaml',
identities => ['AGE-SECRET-KEY-1...'],
);SOPS encrypts values in structured files (YAML, JSON) while keeping keys readable. This enables:
- Git-friendly diffs - see which keys changed without decrypting
- Partial file inspection without full decryption
- Multiple encryption backends (currently age, with PGP/KMS planned)
- MAC verification to detect tampering
database:
password: ENC[AES256_GCM,data:xyz==,iv:abc==,tag:def==,type:str]
host: ENC[AES256_GCM,data:xyz==,iv:abc==,tag:def==,type:str]
sops:
age:
- recipient: age1ql3z7hjy...
enc: |
-----BEGIN AGE ENCRYPTED FILE-----
<encrypted data key>
-----END AGE ENCRYPTED FILE-----
lastmodified: "2025-01-10T12:00:00Z"
mac: ENC[AES256_GCM,data:...,iv:...,tag:...,type:str]
version: 3.7.3Files encrypted with File::SOPS can be decrypted with the reference Go implementation, and vice versa, for YAML and JSON.
Verified against sops v3.13.3 by t/04-interop.t and the interop subtests throughout the suite, which drive the real binary in both directions. They find sops via $SOPS_BIN, then your PATH, then the repo-local .sops-bin/sops, then /tmp/sops (shared logic in t/lib/SopsBin.pm).
Without a binary the suite still reports success — having silently skipped the entire compatibility proof (measured 2026-09-01: it drops a few hundred interop assertions and still prints All tests successful). To install the pinned version where the suite finds it automatically:
maint/fetch-sops .sops-bin # gitignored, survives a /tmp wipe; needs Go or downloads a pinned build
prove -lr t/ # auto-detects .sops-bin/sops -- no PATH or $SOPS_BIN neededKnown limitations:
- Quoted scalars change type on a round trip. sops types a value by what the parser produced; File::SOPS infers the type from the scalar's text. A quoted
"true","false","1"or"0"therefore comes back as a boolean or an integer rather than the string it went in as. Unquoted values are unaffected. - Comments are not preserved.
YAML::XSdiscards them on parse, so they are absent from the output;sops -dkeeps them. - Multi-document YAML is read and written:
decryptreturns an ArrayRef of HashRefs for a stream (a single document stays a HashRef),encryptaccepts one, andextracttakesdocument => $n. One metadata block and one MAC span all documents. Converting a stream to a format with no document stream (JSON, ENV, INI) is refused rather than silently truncated;editon a stream is not enabled yet. - Backends other than age (PGP, KMS, GCP KMS, Azure Key Vault, Vault) are parsed and round-tripped in the metadata but cannot be used for encryption.
cpanm File::SOPSOr manually:
perl Makefile.PL
make
make test
make install- Crypt::Age - age encryption
- CryptX - AES-GCM encryption
- YAML::XS - YAML parsing
- JSON::MaybeXS - JSON parsing
- Moo - OO framework
Currently supported:
- age - Modern encryption using X25519 + ChaCha20-Poly1305
Planned:
- PGP
- AWS KMS
- GCP KMS
- Azure Key Vault
- HashiCorp Vault
- Generate a random 256-bit data key
- Encrypt the data key for each recipient using age
- Encrypt each value with AES-256-GCM using the data key
- Compute MAC over all values for tamper detection
- Store encrypted data keys in
sopsmetadata section
- SOPS - Reference implementation
- Crypt::Age - Perl age encryption
- age - age encryption specification
Torsten Raudssus [email protected]
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.