def encrypt_to_file(contents, filename):
if not filename.endswith('<STR_LIT>'):<EOL><INDENT>raise ValueError("<STR_LIT>" % filename)<EOL><DEDENT>key = Fernet.generate_key()<EOL>fer = Fernet(key)<EOL>encrypted_file = fer.encrypt(contents)<EOL>with open(filename, '<STR_LIT:wb>') as f:<EOL><INDENT>f.write(encrypted_file)<EOL><DEDENT>return key<EOL>
Encrypts ``contents`` and writes it to ``filename``.

``contents`` should be a bytes string. ``filename`` should end with
``.enc``.

Returns the secret key used for the encryption.

Decrypt the file with :func:`doctr.travis.decrypt_file`.