diff --git a/pre_commit_hooks/detect_aws_credentials.py b/pre_commit_hooks/detect_aws_credentials.py
index a744b6f..1663cfd 100644
--- a/pre_commit_hooks/detect_aws_credentials.py
+++ b/pre_commit_hooks/detect_aws_credentials.py
@@ -69,7 +69,7 @@ def get_aws_secrets_from_file(credentials_file: str) -> Set[str]:
 
 def check_file_for_aws_keys(
         filenames: Sequence[str],
-        keys: Set[str],
+        keys: Set[bytes],
 ) -> List[BadFile]:
     """Check if files contain AWS secrets.
 
@@ -79,13 +79,14 @@ def check_file_for_aws_keys(
     bad_files = []
 
     for filename in filenames:
-        with open(filename, 'r') as content:
+        with open(filename, 'rb') as content:
             text_body = content.read()
             for key in keys:
                 # naively match the entire file, low chance of incorrect
                 # collision
                 if key in text_body:
-                    bad_files.append(BadFile(filename, key[:4].ljust(28, '*')))
+                    key_hidden = key.decode()[:4].ljust(28, '*')
+                    bad_files.append(BadFile(filename, key_hidden))
     return bad_files
 
 
@@ -137,7 +138,8 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
         )
         return 2
 
-    bad_filenames = check_file_for_aws_keys(args.filenames, keys)
+    keys_b = {key.encode() for key in keys}
+    bad_filenames = check_file_for_aws_keys(args.filenames, keys_b)
     if bad_filenames:
         for bad_file in bad_filenames:
             print(f'AWS secret found in {bad_file.filename}: {bad_file.key}')

UnicodeDecodeError in detect-aws-credentials is unreadable
```
Detect AWS Credentials...................................................Failed
hookid: detect-aws-credentials

Traceback (most recent call last):
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/bin/detect-aws-credentials", line 11, in <module>
    sys.exit(main())
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/lib/python3.7/site-packages/pre_commit_hooks/detect_aws_credentials.py", line 133, in main
    bad_filenames = check_file_for_aws_keys(args.filenames, keys)
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/lib/python3.7/site-packages/pre_commit_hooks/detect_aws_credentials.py", line 74, in check_file_for_aws_keys
    text_body = content.read()
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xeb in position 103: invalid continuation byte
Traceback (most recent call last):
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/bin/detect-aws-credentials", line 11, in <module>
    sys.exit(main())
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/lib/python3.7/site-packages/pre_commit_hooks/detect_aws_credentials.py", line 133, in main
    bad_filenames = check_file_for_aws_keys(args.filenames, keys)
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/lib/python3.7/site-packages/pre_commit_hooks/detect_aws_credentials.py", line 74, in check_file_for_aws_keys
    text_body = content.read()
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 2346: invalid start byte
Traceback (most recent call last):
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/bin/detect-aws-credentials", line 11, in <module>
    sys.exit(main())
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/lib/python3.7/site-packages/pre_commit_hooks/detect_aws_credentials.py", line 133, in main
    bad_filenames = check_file_for_aws_keys(args.filenames, keys)
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/lib/python3.7/site-packages/pre_commit_hooks/detect_aws_credentials.py", line 74, in check_file_for_aws_keys
    text_body = content.read()
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 187: invalid continuation byte
Traceback (most recent call last):
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/bin/detect-aws-credentials", line 11, in <module>
    sys.exit(main())
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/lib/python3.7/site-packages/pre_commit_hooks/detect_aws_credentials.py", line 133, in main
    bad_filenames = check_file_for_aws_keys(args.filenames, keys)
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/lib/python3.7/site-packages/pre_commit_hooks/detect_aws_credentials.py", line 74, in check_file_for_aws_keys
    text_body = content.read()
  File "/home/ryan/.cache/pre-commit/repo8jj7q4iz/py_env-python3.7/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 167: invalid continuation byte
```

It rejected the commit, so that's good. And I'm not sure if I can recommend any improvements to the handling of invalid unicode since I haven't looked at the code in this module.

But for sure this error message could be more useful. There are 773 files in this commit and it would sure be nice to know which one(s) have this problem.

Edit: Ideally if there's an exception it would continue to the next file so all the files that couldn't be processed can be listed to the user before the commit is rejected. I'm not looking forward to fixing one file, doing `git commit`, fixing another... 500 times. ;)
diff --git a/tests/detect_aws_credentials_test.py b/tests/detect_aws_credentials_test.py
index 46e5b36..41b7b0a 100644
--- a/tests/detect_aws_credentials_test.py
+++ b/tests/detect_aws_credentials_test.py
@@ -117,6 +117,19 @@ def test_detect_aws_credentials(filename, expected_retval):
     assert ret == expected_retval
 
 
+def test_allows_arbitrarily_encoded_files(tmpdir):
+    src_ini = tmpdir.join('src.ini')
+    src_ini.write(
+        '[default]\n'
+        'aws_access_key_id=AKIASDFASDF\n'
+        'aws_secret_Access_key=9018asdf23908190238123\n',
+    )
+    arbitrary_encoding = tmpdir.join('f')
+    arbitrary_encoding.write_binary(b'\x12\x9a\xe2\xf2')
+    ret = main((str(arbitrary_encoding), '--credentials-file', str(src_ini)))
+    assert ret == 0
+
+
 @patch('pre_commit_hooks.detect_aws_credentials.get_aws_secrets_from_file')
 @patch('pre_commit_hooks.detect_aws_credentials.get_aws_secrets_from_env')
 def test_non_existent_credentials(mock_secrets_env, mock_secrets_file, capsys):

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest
3.9
requirements-dev.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
cfgv==3.4.0
coverage==7.8.0
distlib==0.3.9
exceptiongroup==1.2.2
filelock==3.18.0
flake8==7.2.0
identify==2.6.9
iniconfig==2.1.0
mccabe==0.7.0
mock==5.2.0
nodeenv==1.9.1
packaging==24.2
platformdirs==4.3.7
pluggy==1.5.0
pre_commit==4.2.0
-e git+https://github.com/pre-commit/pre-commit-hooks.git@c7d0d3c9cc59ab05329e69276afd84a1e87b0c0d#egg=pre_commit_hooks
pycodestyle==2.13.0
pyflakes==3.3.1
pytest==8.3.5
PyYAML==6.0.2
ruamel.yaml==0.18.10
ruamel.yaml.clib==0.2.12
toml==0.10.2
tomli==2.2.1
virtualenv==20.29.3

name: pre-commit-hooks
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - cfgv==3.4.0
      - coverage==7.8.0
      - distlib==0.3.9
      - exceptiongroup==1.2.2
      - filelock==3.18.0
      - flake8==7.2.0
      - identify==2.6.9
      - iniconfig==2.1.0
      - mccabe==0.7.0
      - mock==5.2.0
      - nodeenv==1.9.1
      - packaging==24.2
      - platformdirs==4.3.7
      - pluggy==1.5.0
      - pre-commit==4.2.0
      - pycodestyle==2.13.0
      - pyflakes==3.3.1
      - pytest==8.3.5
      - pyyaml==6.0.2
      - ruamel-yaml==0.18.10
      - ruamel-yaml-clib==0.2.12
      - toml==0.10.2
      - tomli==2.2.1
      - virtualenv==20.29.3
prefix: /opt/conda/envs/pre-commit-hooks

tests/detect_aws_credentials_test.py::test_allows_arbitrarily_encoded_files
tests/detect_aws_credentials_test.py::test_get_aws_credentials_file_from_env[env_vars0-values0] tests/detect_aws_credentials_test.py::test_get_aws_credentials_file_from_env[env_vars1-values1] tests/detect_aws_credentials_test.py::test_get_aws_credentials_file_from_env[env_vars2-values2] tests/detect_aws_credentials_test.py::test_get_aws_credentials_file_from_env[env_vars3-values3] tests/detect_aws_credentials_test.py::test_get_aws_credentials_file_from_env[env_vars4-values4] tests/detect_aws_credentials_test.py::test_get_aws_credentials_file_from_env[env_vars5-values5] tests/detect_aws_credentials_test.py::test_get_aws_credentials_file_from_env[env_vars6-values6] tests/detect_aws_credentials_test.py::test_get_aws_credentials_file_from_env[env_vars7-values7] tests/detect_aws_credentials_test.py::test_get_aws_credentials_file_from_env[env_vars8-values8] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_env[env_vars0-values0] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_env[env_vars1-values1] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_env[env_vars2-values2] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_env[env_vars3-values3] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_env[env_vars4-values4] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_env[env_vars5-values5] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_env[env_vars6-values6] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_env[env_vars7-values7] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_env[env_vars8-values8] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_file[aws_config_with_secret.ini-expected_keys0] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_file[aws_config_with_session_token.ini-expected_keys1] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_file[aws_config_with_secret_and_session_token.ini-expected_keys2] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_file[aws_config_with_multiple_sections.ini-expected_keys3] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_file[aws_config_without_secrets.ini-expected_keys4] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_file[aws_config_without_secrets_with_spaces.ini-expected_keys5] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_file[nonsense.txt-expected_keys6] tests/detect_aws_credentials_test.py::test_get_aws_secrets_from_file[ok_json.json-expected_keys7] tests/detect_aws_credentials_test.py::test_detect_aws_credentials[aws_config_with_secret.ini-1] tests/detect_aws_credentials_test.py::test_detect_aws_credentials[aws_config_with_session_token.ini-1] tests/detect_aws_credentials_test.py::test_detect_aws_credentials[aws_config_with_multiple_sections.ini-1] tests/detect_aws_credentials_test.py::test_detect_aws_credentials[aws_config_without_secrets.ini-0] tests/detect_aws_credentials_test.py::test_detect_aws_credentials[aws_config_without_secrets_with_spaces.ini-0] tests/detect_aws_credentials_test.py::test_detect_aws_credentials[nonsense.txt-0] tests/detect_aws_credentials_test.py::test_detect_aws_credentials[ok_json.json-0] tests/detect_aws_credentials_test.py::test_non_existent_credentials tests/detect_aws_credentials_test.py::test_non_existent_credentials_with_allow_flag
pre_commit_hooks/detect_aws_credentials.py
pre_commit_hooks/detect_aws_credentials.py