Skip to content

Instantly share code, notes, and snippets.

@t1m0thyj
Last active August 2, 2023 20:15
Show Gist options
  • Save t1m0thyj/bdd8901f35d36858e307fdbdb77528f0 to your computer and use it in GitHub Desktop.
Save t1m0thyj/bdd8901f35d36858e307fdbdb77528f0 to your computer and use it in GitHub Desktop.
Test UTF-16 and long credentials in Windows keyring
{
"name": "keyring-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"keytar": "^7.9.0"
}
}
const keytar = require("keytar");
const password = "πŸŒžπŸŒ™πŸŒŸπŸŒ΄\f";
(async () => {
console.log("Password:", Buffer.from(password));
await keytar.setPassword("Zowe", "test_secret_js", password);
console.log("JS -> JS:", await keytar.getPassword("Zowe", "test_secret_js"));
console.log("PY -> JS:", await keytar.getPassword("Zowe", "test_secret_py"));
})();
import keyring
def get_password_js(service, account):
# Load password in format stored by Node.js Keytar
password = keyring.get_password(f"{service}/{account}", account)
try:
return password.encode("utf-16le").decode()
except UnicodeDecodeError:
return password
def set_password_js(service, account, password):
# Store password in format compatible with Node.js Keytar
if len(password) % 2 == 1:
password += "\0"
password = password.encode().decode("utf-16le")
keyring.set_password(f"{service}/{account}", account, password)
# Test storing credential with UTF-16 and control chars
password = "πŸŒžπŸŒ™πŸŒŸπŸŒ΄\f"
print("Password:", password.encode())
set_password_js("Zowe", "test_secret_py", password)
print("JS -> PY:", get_password_js("Zowe", "test_secret_js"))
print("PY -> PY:", get_password_js("Zowe", "test_secret_py"))
# Test storing long credential that is 2560 chars
set_password_js("Zowe", "test_secret_long", "abcde" * 512)
assert len(get_password_js("Zowe", "test_secret_long")) == 2560
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment