I need to check if a geohash string is valid, so I need to check if it's a base32 or not.
            Asked
            
        
        
            Active
            
        
            Viewed 3,980 times
        
    2
            
            
        - 
                    Can you add some more content to this question, to show us what you've already tried to solve this problem, and what issues you've run into? – Rob Wilkins Dec 03 '18 at 21:29
- 
                    This answer is not perfect, but the better I've found https://stackoverflow.com/a/27362880/2093371 – hestellezg Jan 23 '21 at 06:49
3 Answers
2
            Base32 uses A-Z and 2-7 for the encoding, and adds a padding character = to get a multiple of 8 characters, so you can create a regex to see if the candidate string matches.
Using regex.exec a matching string will return the match information, a non-matching string will return null, so you can use an if to test whether a match is true or false.
Base32 encodings also must always be a length that is a multiple of 8, and are padded with enough = chars to make it so; you can check the length is correct by using mod 8 --
if (str.length % 8 === 0) { /* then ok */ }
// A-Z and 2-7 repeated, with optional `=` at the end
let b32_regex = /^[A-Z2-7]+=*$/;
var b32_yes = 'AJU3JX7ZIA54EZQ=';
var b32_no  = 'klajcii298slja018alksdjl';
    
if (b32_yes.length % 8 === 0 &&
    b32_regex.exec(b32_yes)) {
    console.log("this one is base32");
}
else {
    console.log("this one is NOT base32");
}
    
if (b32_no % 8 === 0 &&
    b32_regex.exec(b32_no)) {
    console.log("this one is base32");
}
else {
    console.log("this one is NOT base32");
} 
    
    
        Stephen P
        
- 14,422
- 2
- 43
- 67
- 
                    1
- 
                    Thanks @Zim — updated to include optional padding. Length checks could also be done, since the length must be a multiple of 8 `b32_yes % 8 === 0 and b32_regex.exec(...)` – Stephen P Dec 03 '18 at 20:42
2
            
            
        function isBase32(input) {
    const regex = /^([A-Z2-7=]{8})+$/
    return regex.test(input)
}
console.log(isBase32('ABCDE23=')) //true
console.log(isBase32('aBCDE23=')) //false
console.log(isBase32('')) //false
console.log(isBase32()) //false
console.log(isBase32(null)) //false
console.log(isBase32('ABCDE567ABCDE2==')) //true
console.log(isBase32('NFGH@#$aBCDE23==')) //false0
            
            
        //I put together this little HTML & js button to mess around with.
<!DOCTYPE html>
<html>
  <head>
    <title>Base32/Base64 Checker</title>
  </head>
  <body>
    <h1>Base32/Base64 Checker</h1>
    <label for="input">Enter a string:</label>
    <input type="text" id="input" name="input"><br><br>
    <button onclick="check()">Check</button>
    <p id="result"></p>
    <script>
      function check() {
        var input = document.getElementById("input").value;
        var isBase32 = /^[A-Z2-7]+=*$/.test(input);
        var isBase64 = /^[A-Za-z0-9+/]+=*$/i.test(input);
        var result;
        if (isBase32) {
          result = "The input is Base32 encoded.";
        } else if (isBase64) {
          result = "The input is Base64 encoded.";
        } else {
          result = "Neither Base32 nor Base64 !.";
        }
        document.getElementById("result").innerHTML = result;
      }
    </script>
  </body>
</html>
 
    
    
        MarchonaX
        
- 26
- 2
