Last active
June 3, 2023 10:49
-
-
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
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
'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')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Expected output: