kcpasswd
v1.1.1
Published
Library for encrypting and decrypting passwords using the Keycloak password hashing algorithm in TypeScript.
Readme
kcpasswd TypeScript
Library for encrypting and decrypting passwords using the Keycloak password hashing algorithm in TypeScript.
Installation
npm install kcpasswdUsage
import { encrypt, decrypt } from 'kcpasswd';
// Encrypt a password
const encryptedPassword = encrypt('my-secret-password');
// Decrypt a password
const decryptedPassword = decrypt(encryptedPassword);
console.log('Encrypted:', encryptedPassword);
console.log('Decrypted:', decryptedPassword);C Implementation
The project comes with a compiled C binary that can be used to encrypt and decrypt passwords. The binary is located in the bin directory.
Below source code in C:
main.c
#include <stdio.h>
#include <string.h>
#define MAXLEN 256
int readfile(char* path, char* buf) {
FILE* fd;
fd = fopen(path, "r");
if(!fd)
return -1;
fgets(buf, MAXLEN, fd);
fclose(fd);
return 0;
}
void applexor(char* msg, char* out) {
int keys[] = {125, 137, 82, 35, 210, 188, 221, 234, 163, 185, 31};
int i = 0;
// Encrypt/decrypt (xor)
for(i=0; msg[i] != '\0'; i++)
out[i] = msg[i] ^ keys[i % 11];
// If the previous char wasn't a null byte, we are likely encrypting not
// decrypting, so add a null byte.
if(out[i-1] != '\0') {
out[i] = '\0' ^ keys[i % 11];
i++;
}
// Append until we hit a multiple of 12
for(; i%12 != 0; i++)
out[i] = msg[i%strlen(msg)] ^ keys[i % 11];
out[i] = '\0' ;
}
int main(int argc, char* argv[]) {
char in[MAXLEN];
char out[MAXLEN];
if(argc == 1) {
printf("Must provide path to file or a password to encrypt\n");
return 1;
}
// If the file can't be opened, try to use the "path" as a password
if(readfile(argv[1], in) == -1)
strcpy(in, argv[1]);
applexor(in, out);
printf("%s\n", out);
return 0;
}Makefile
CCOPTS = --std=c99 -Wall -Werror -O2
all:
cc $(CCOPTS) -o kcpasswd main.cUsage
# Compile the C code
make
# Encrypt a password
./kcpasswd "<account password>"
# Encrypt a password and save into MAC for auto-login
./kcpasswd "<account password>" | sudo tee /etc/kcpassword
sudo /usr/bin/defaults write /Library/Preferences/com.apple.loginwindow autoLoginUser "<account username>"Credits
This project is inspired by the Keycloak password hashing algorithm and the original implementation in C. The TypeScript version is a port of the original C code, providing a convenient way to encrypt and decrypt passwords in a Node.js environment.
