Last active
August 2, 2023 20:15
-
-
Save t1m0thyj/bdd8901f35d36858e307fdbdb77528f0 to your computer and use it in GitHub Desktop.
Test UTF-16 and long credentials in Windows keyring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
keyring~=24.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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