Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlorianWolters/419e8b999bf00c9c01310ea5782aa986 to your computer and use it in GitHub Desktop.
Save FlorianWolters/419e8b999bf00c9c01310ea5782aa986 to your computer and use it in GitHub Desktop.
Demonstrates how to handle an input string that can be optionally encoded with Base64 with Node.js v20.2.0
'use strict';
function toUtf8FromPotentialBase64Encoded(input) {
const buffer = Buffer.from(input, 'base64');
return (input === buffer.toString('base64'))
? buffer.toString('utf-8')
: input;
}
// Usage
console.log('Base64 encoded DN is...: "' + toUtf8FromPotentialBase64Encoded('bj1Kw7NobiBEw7ZlLG91PUhleMO2cixvdT1BZG1pbmlzdHJhdG9yLGRjPWV4YW1wbGUsZGM9ZGU='));
console.log('Plain DN is............: "' + toUtf8FromPotentialBase64Encoded('n=Jóhn Döe,ou=Hexör,ou=Administrator,dc=example,dc=de'));
@FlorianWolters
Copy link
Author

Expected output:

Base64 encoded DN is...: "n=Jóhn Döe,ou=Hexör,ou=Administrator,dc=example,dc=de
Plain DN is............: "n=Jóhn Döe,ou=Hexör,ou=Administrator,dc=example,dc=de

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment