Last active
March 30, 2020 10:15
-
-
Save chris-scientist/f52e3900c09d39fe142721ee9008346b to your computer and use it in GitHub Desktop.
Encode string in Java
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
package io.github.chrisscientist.encode; | |
import java.nio.charset.StandardCharsets; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.Arrays; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* Encode string with SHA256 ! | |
* | |
* @see https://docs.oracle.com/javase/7/docs/api/java/security/MessageDigest.html | |
* @see https://stackoverflow.com/questions/5531455/how-to-hash-some-string-with-sha256-in-java | |
* @see https://howtodoinjava.com/array/convert-byte-array-string-vice-versa/ | |
* @author Christopher | |
*/ | |
public class EncodeStringMain { | |
private static String ALGORITHM = "SHA-256"; | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
try { | |
System.out.println("[" + getDate() + "] Encode a string with " + ALGORITHM); | |
encodeWithSha256Tests(); | |
} catch (CloneNotSupportedException ex) { | |
Logger.getLogger(EncodeSringMain.class.getName()).log(Level.SEVERE, null, ex); | |
ex.printStackTrace(); | |
} catch (NoSuchAlgorithmException ex) { | |
Logger.getLogger(EncodeSringMain.class.getName()).log(Level.SEVERE, null, ex); | |
ex.printStackTrace(); | |
} | |
} | |
private static String getDate() { | |
Date today = Calendar.getInstance().getTime(); | |
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm"); | |
return dateFormat.format(today); | |
} | |
private static void encodeWithSha256Tests() throws CloneNotSupportedException, NoSuchAlgorithmException { | |
// String to encode | |
String strAlpha = "A thing!"; | |
String strBeta = "An other thing!"; | |
String inputTextAlpha = strAlpha; | |
String inputTextBeta = strBeta; | |
String inputTextAlphaCopy = strAlpha; | |
// Encode inputTextAlpha | |
byte[] itaEncoded = encodeString(inputTextAlpha); | |
System.out.println( | |
"Alpha : '" + inputTextAlpha | |
+ "' encoded in : " + (new String(itaEncoded, StandardCharsets.UTF_8)) + " {" + transformByteArray(itaEncoded) + "}" | |
); | |
// Encode inputTextBeta | |
byte[] itbEncoded = encodeString(inputTextBeta); | |
System.out.println( | |
"Beta : '" + inputTextBeta | |
+ "' encoded in : " + (new String(itbEncoded, StandardCharsets.UTF_8)) + " {" + transformByteArray(itbEncoded) + "}" | |
); | |
// Encode inputTextAlpha | |
byte[] itacpEncoded = encodeString(inputTextAlphaCopy); | |
System.out.println( | |
"Alpha cp : '" + inputTextAlphaCopy | |
+ "' encoded in : " + (new String(itacpEncoded, StandardCharsets.UTF_8)) + " {" + transformByteArray(itacpEncoded) + "}" | |
); | |
// compare encode | |
String shaIta = ALGORITHM + "(" + inputTextAlpha + ")"; | |
String shaItb = ALGORITHM + "(" + inputTextBeta + ")"; | |
String shaItacp = ALGORITHM + "(" + inputTextAlphaCopy + ")"; | |
// a compare at b | |
if(Arrays.equals(itaEncoded, itbEncoded)) { | |
System.out.println(shaIta + " == " + shaItb); | |
} else { | |
System.out.println(shaIta + " != " + shaItb); | |
} | |
// a cp compare at b | |
if(Arrays.equals(itacpEncoded, itbEncoded)) { | |
System.out.println(shaItacp + " == " + shaItb); | |
} else { | |
System.out.println(shaItacp + " != " + shaItb); | |
} | |
// a compare at a cp | |
if(Arrays.equals(itaEncoded, itacpEncoded)) { | |
System.out.println(shaIta + " == " + shaItacp); | |
} else { | |
System.out.println(shaIta + " != " + shaItacp); | |
} | |
} | |
private static byte[] encodeString(String inputString) throws NoSuchAlgorithmException, CloneNotSupportedException { | |
MessageDigest md = MessageDigest.getInstance(ALGORITHM); | |
md.update(inputString.getBytes(StandardCharsets.UTF_8)); | |
MessageDigest mdCp = (MessageDigest) md.clone(); | |
byte[] outputEncoded = mdCp.digest(); | |
return outputEncoded; | |
} | |
private static String transformByteArray(byte[] anArray) { | |
String output = ""; | |
for(byte nb : anArray) { | |
if(!"".equals(output)) { | |
output += ";"; | |
} | |
output += String.valueOf((int)nb); | |
} | |
return output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment