diff --git a/src/main/kotlin/com/klaxit/hiddensecrets/Utils.kt b/src/main/kotlin/com/klaxit/hiddensecrets/Utils.kt index 418d7ae..abade11 100644 --- a/src/main/kotlin/com/klaxit/hiddensecrets/Utils.kt +++ b/src/main/kotlin/com/klaxit/hiddensecrets/Utils.kt @@ -26,11 +26,11 @@ object Utils { * Encode string to sha256 */ @VisibleForTesting - fun sha256(toHash: String): String { + fun sha256(toHash: String): ByteArray { val bytes = toHash.toByteArray() val md = MessageDigest.getInstance("SHA-256") val digest = md.digest(bytes) - return digest.fold("") { str, it -> str + "%02x".format(it) } + return digest } /** @@ -38,8 +38,7 @@ object Utils { */ fun encodeSecret(key: String, packageName: String): String { // Generate the obfuscator as the SHA256 of the app package name - val obfuscator = sha256(packageName) - val obfuscatorBytes = obfuscator.toByteArray() + val obfuscatorBytes = sha256(packageName) // Generate the obfuscated secret bytes array by applying a XOR between the secret and the obfuscator val obfuscatedSecretBytes = arrayListOf() diff --git a/src/main/resources/cpp/secrets.cpp b/src/main/resources/cpp/secrets.cpp index a881813..ae0dbbd 100644 --- a/src/main/resources/cpp/secrets.cpp +++ b/src/main/resources/cpp/secrets.cpp @@ -45,7 +45,7 @@ jstring getOriginalKey( // Get the obfuscating string SHA256 as the obfuscator const char *obfuscatingStr = pEnv->GetStringUTFChars(obfuscatingJStr, NULL); - char buffer[2 * SHA256::DIGEST_SIZE + 1]; + char buffer[SHA256::DIGEST_SIZE]; sha256(obfuscatingStr, buffer); const char *obfuscator = buffer; @@ -53,7 +53,7 @@ jstring getOriginalKey( // Apply a XOR between the obfuscated key and the obfuscating string to get original string char out[obfuscatedSecretSize + 1]; for (int i = 0; i < obfuscatedSecretSize; i++) { - out[i] = obfuscatedSecret[i] ^ obfuscator[i % strlen(obfuscator)]; + out[i] = obfuscatedSecret[i] ^ obfuscator[i % SHA256::DIGEST_SIZE]; } // Add string terminal delimiter diff --git a/src/main/resources/cpp/sha256.cpp b/src/main/resources/cpp/sha256.cpp index b949a67..b1511cc 100644 --- a/src/main/resources/cpp/sha256.cpp +++ b/src/main/resources/cpp/sha256.cpp @@ -150,7 +150,7 @@ void SHA256::final(unsigned char *digest) } } -void sha256(const char* input, char buf[2*SHA256::DIGEST_SIZE + 1]) +void sha256(const char* input, char buf[SHA256::DIGEST_SIZE]) { unsigned char digest[SHA256::DIGEST_SIZE]; memset(digest, 0, SHA256::DIGEST_SIZE); @@ -160,9 +160,6 @@ void sha256(const char* input, char buf[2*SHA256::DIGEST_SIZE + 1]) ctx.update( (unsigned char*)input, strlen(input)); ctx.final(digest); - buf[2*SHA256::DIGEST_SIZE] = 0; - for (int i = 0; i < SHA256::DIGEST_SIZE; i++) { - sprintf(buf + i * 2, "%02x", digest[i]); - } + memcpy(buf, digest, SHA256::DIGEST_SIZE); } diff --git a/src/test/kotlin/HiddenSecretsTest.kt b/src/test/kotlin/HiddenSecretsTest.kt index b5eecac..f301635 100644 --- a/src/test/kotlin/HiddenSecretsTest.kt +++ b/src/test/kotlin/HiddenSecretsTest.kt @@ -45,7 +45,7 @@ class HiddenSecretsTest : WordSpec({ val result = gradleRunner.withArguments(HiddenSecretsPlugin.TASK_OBFUSCATE, "-Pkey=$key", "-Ppackage=$packageName").build() println(result.output) // Should contain obfuscated key - result.output shouldContain "{ 0x15, 0x58, 0xb, 0x43, 0x78, 0x4a, 0x23, 0x6d, 0x1, 0x4b, 0x46, 0x7c, 0x57, 0x41 }" + result.output shouldContain "{ 0xd4, 0xd8, 0x70, 0xca, 0x91, 0x54, 0x69, 0x77, 0xc4, 0xb6, 0x99, 0x5f, 0xb8, 0x98 }" } "Make command ${HiddenSecretsPlugin.TASK_PACKAGE_NAME} succeed" { diff --git a/src/test/kotlin/UtilsTest.kt b/src/test/kotlin/UtilsTest.kt index 8f4b2e1..9c47870 100644 --- a/src/test/kotlin/UtilsTest.kt +++ b/src/test/kotlin/UtilsTest.kt @@ -8,6 +8,8 @@ import java.io.File */ class UtilsTest : WordSpec({ + fun byteArrayOfInts(vararg ints: Int) = ByteArray(ints.size) { pos -> ints[pos].toByte() } + val packageName = "com.klaxit.test" "Using getCppPackageName()" should { @@ -25,7 +27,7 @@ class UtilsTest : WordSpec({ "Using sha256()" should { "encode String in sha256" { val key = "youCanNotFindMySecret!" - Utils.sha256(key) shouldBe "7bdc2b5992ef7b4cce0e06295f564f4fad0c96e5f82a0bcf9cd8323d3a3bcfbd" + Utils.sha256(key) shouldBe byteArrayOfInts( 0x7b, 0xdc, 0x2b, 0x59, 0x92, 0xef, 0x7b, 0x4c, 0xce, 0x0e, 0x06, 0x29, 0x5f, 0x56, 0x4f, 0x4f, 0xad, 0x0c, 0x96, 0xe5, 0xf8, 0x2a, 0x0b, 0xcf, 0x9c, 0xd8, 0x32, 0x3d, 0x3a, 0x3b, 0xcf, 0xbd) /*"7bdc2b5992ef7b4cce0e06295f564f4fad0c96e5f82a0bcf9cd8323d3a3bcfbd"*/ } } @@ -35,14 +37,14 @@ class UtilsTest : WordSpec({ Utils.encodeSecret( key, packageName - ) shouldBe "{ 0x5b, 0x6, 0x18, 0x31, 0xb, 0x72, 0x57, 0x5, 0x5d, 0x57, 0x3 }" + ) shouldBe "{ 0x67, 0xcb, 0xae, 0xcb, 0x4c, 0xbb, 0x42, 0xad, 0x59, 0x19, 0xe2 }" //"{ 0x5b, 0x6, 0x18, 0x31, 0xb, 0x72, 0x57, 0x5, 0x5d, 0x57, 0x3 }" } "encode String with special characters" { val key = "@&é(§èçà)-ù,;:=#°_*%£?./+" Utils.encodeSecret( key, packageName - ) shouldBe "{ 0x70, 0x45, 0xa2, 0xcc, 0x4c, 0xf5, 0x9e, 0xa5, 0x9a, 0xf0, 0xc1, 0xa6, 0x92, 0x4a, 0x4e, 0xa6, 0x8a, 0x1a, 0xc, 0x5e, 0x5, 0x14, 0xf7, 0x86, 0x6b, 0x13, 0x40, 0xf5, 0x9a, 0xc, 0x16, 0x16, 0x19 }" + ) shouldBe "{ 0x4c, 0x88, 0x14, 0x36, 0xb, 0x3c, 0x8b, 0xd, 0x9e, 0xbe, 0x20, 0x95, 0xe9, 0xce, 0xbe, 0x4a, 0x94, 0xf1, 0xb2, 0x7d, 0x4c, 0x70, 0x51, 0x91, 0x69, 0x98, 0x4d, 0xf7, 0x8a, 0xbc, 0xb1, 0xa2, 0x27 }" } }