From a872e66d63b05ff79ee9549f285e79912e145f8a Mon Sep 17 00:00:00 2001 From: KKlochko Date: Fri, 1 Dec 2023 18:43:44 +0200 Subject: [PATCH] Refactor the SimpleToken factory to move hash function to Sha256Hash. --- .../security/hashes/Sha256Hash.java | 8 ++++++ .../security/tokens/SimpleToken.java | 4 ++- .../security/hashes/Sha256HashSpec.groovy | 27 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/main/java/space/kklochko/spring_rest_example/security/hashes/Sha256Hash.java create mode 100644 src/test/groovy/space/kklochko/spring_rest_example/security/hashes/Sha256HashSpec.groovy diff --git a/src/main/java/space/kklochko/spring_rest_example/security/hashes/Sha256Hash.java b/src/main/java/space/kklochko/spring_rest_example/security/hashes/Sha256Hash.java new file mode 100644 index 0000000..689afdc --- /dev/null +++ b/src/main/java/space/kklochko/spring_rest_example/security/hashes/Sha256Hash.java @@ -0,0 +1,8 @@ +package space.kklochko.spring_rest_example.security.hashes; + +public class Sha256Hash { + public String hashOf(String input) { + return org.apache.commons.codec.digest.DigestUtils.sha256Hex(input); + } +} + diff --git a/src/main/java/space/kklochko/spring_rest_example/security/tokens/SimpleToken.java b/src/main/java/space/kklochko/spring_rest_example/security/tokens/SimpleToken.java index 520f9c1..d98fe0a 100644 --- a/src/main/java/space/kklochko/spring_rest_example/security/tokens/SimpleToken.java +++ b/src/main/java/space/kklochko/spring_rest_example/security/tokens/SimpleToken.java @@ -1,8 +1,10 @@ package space.kklochko.spring_rest_example.security.tokens; +import space.kklochko.spring_rest_example.security.hashes.Sha256Hash; + public class SimpleToken extends StringTokenFactory { public String create(String input) { - return org.apache.commons.codec.digest.DigestUtils.sha256Hex(input); + return (new Sha256Hash()).hashOf(input); } } diff --git a/src/test/groovy/space/kklochko/spring_rest_example/security/hashes/Sha256HashSpec.groovy b/src/test/groovy/space/kklochko/spring_rest_example/security/hashes/Sha256HashSpec.groovy new file mode 100644 index 0000000..32c1411 --- /dev/null +++ b/src/test/groovy/space/kklochko/spring_rest_example/security/hashes/Sha256HashSpec.groovy @@ -0,0 +1,27 @@ +package space.kklochko.spring_rest_example.security.hashes + + +import spock.lang.Specification; +import spock.lang.Subject; + +public class Sha256HashSpec extends Specification { + def "Create a token that based on sha256"() { + given: "I have a hasher" + @Subject + def hasher = new Sha256Hash() + + when: "I get a hash of a string" + String hash = hasher.hashOf(input) + + then: "The token is the same if the same input data." + expectedHash == hash + + where: "I have the expected data" + input || expectedHash + "abc" || "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" + "random numbers232341" || "7f4b80b2b09ae4326f78b1a9782c807af0a7d0194bffb05602a6585466b4c43e" + "admin" || "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918" + "user" || "04f8996da763b7a969b1028ee3007569eaf3a635486ddab211d512c85b9df8fb" + } +} +