Last active
June 18, 2021 11:02
-
-
Save kcak11/e86dc49ad8ca41cb6da21f51a0a6b1ba to your computer and use it in GitHub Desktop.
StringObfuscator
This file contains hidden or 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
package com.string.utils; | |
import java.io.UnsupportedEncodingException; | |
import java.net.URLEncoder; | |
import java.util.Base64; | |
public class StringObfuscator { | |
private StringObfuscator() {} | |
public static String obfuscate(String input) { | |
String b64str = Base64.getEncoder().encodeToString(encodeURIComponent(input.toLowerCase()).getBytes()); | |
b64str = b64str.replace("+", "").replace("=", "").replace("/", ""); | |
Integer sum = 0; | |
int index = 0; | |
for (int i = 0; i < b64str.length() - 4; i += 4) { | |
sum += Integer.parseInt(b64str.substring(i, i + 4), 36); | |
index = i + 4; | |
} | |
sum += Integer.parseInt(b64str.substring(index), 36); | |
return Integer.toString(sum, 36); | |
} | |
public static String encodeURIComponent(String s) { | |
String result = null; | |
try { | |
result = URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "%20").replaceAll("\\%21", "!").replaceAll("\\%27", "'").replaceAll("\\%28", "(").replaceAll("\\%29", ")").replaceAll("\\%7E", "~"); | |
} catch (UnsupportedEncodingException e) { | |
result = s; | |
} | |
return result; | |
} | |
} |
This file contains hidden or 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
function obfuscate(str) { | |
if (!str) { | |
throw new Error("Invalid string input specified."); | |
} | |
let _str = btoa(encodeURIComponent(str.toLowerCase())); | |
_str = _str.replace(/\//g, "").replace(/\+/g, "").replace(/\=/g, ""); | |
let _sum = 0; | |
for (let i = 0; i < _str.length; i += 4) { | |
_sum += parseInt(_str.substr(i, 4), 36); | |
} | |
return _sum.toString(36); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment