-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MOB-22458][MOB-22611] Cleanup StringEncoder util (#729)
- Loading branch information
Showing
11 changed files
with
167 additions
and
282 deletions.
There are no files selected for viewing
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
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
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
50 changes: 50 additions & 0 deletions
50
code/core/src/main/java/com/adobe/marketing/mobile/internal/util/StringExtensions.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright 2024 Adobe. All rights reserved. | ||
This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. You may obtain a copy | ||
of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software distributed under | ||
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
OF ANY KIND, either express or implied. See the License for the specific language | ||
governing permissions and limitations under the License. | ||
*/ | ||
|
||
package com.adobe.marketing.mobile.internal.util | ||
|
||
/** | ||
* Computes the signed 2's complement FNV-1a 32-bit hash for the given string input. | ||
* | ||
* <p>https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function Online | ||
* validator - https://md5calc.com/hash/fnv1a32?str= | ||
* | ||
* @param input The string to be hashed. | ||
* @return A 32-bit signed integer representing the FNV-1a hash of the input string. | ||
*/ | ||
private fun computeFnv1aHash(input: String?): Int { | ||
val ALL_BITS_ENABLED = 0xFF | ||
val PRIME = 0x1000193 // 16777619 as hex | ||
val OFFSET = -0x7ee3623b // 2166136261 as hex | ||
|
||
if (input == null) { | ||
return 0 | ||
} | ||
|
||
var hash = if (input.trim().isEmpty()) 0 else OFFSET | ||
val bytes = input.toByteArray() | ||
|
||
for (aByte in bytes) { | ||
hash = hash xor (aByte.toInt() and ALL_BITS_ENABLED) | ||
hash *= PRIME | ||
} | ||
|
||
return hash | ||
} | ||
|
||
/** | ||
* Extension function that computes the decimal FNV-1a 32-bit hash for a string. | ||
* | ||
* @return The decimal representation of the FNV-1a 32-bit hash. | ||
*/ | ||
internal fun String?.fnv1a32(): Long { | ||
return computeFnv1aHash(this).toUInt().toLong() | ||
} |
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
56 changes: 56 additions & 0 deletions
56
code/core/src/main/java/com/adobe/marketing/mobile/util/StringEncoder.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
Copyright 2024 Adobe. All rights reserved. | ||
This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. You may obtain a copy | ||
of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software distributed under | ||
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
OF ANY KIND, either express or implied. See the License for the specific language | ||
governing permissions and limitations under the License. | ||
*/ | ||
|
||
package com.adobe.marketing.mobile.util | ||
|
||
import com.adobe.marketing.mobile.internal.CoreConstants | ||
import com.adobe.marketing.mobile.services.Log | ||
import java.nio.charset.StandardCharsets | ||
import java.security.MessageDigest | ||
import java.security.NoSuchAlgorithmException | ||
|
||
object StringEncoder { | ||
private const val LOG_TAG = "StringEncoder" | ||
private const val LSB_8_MASK = 0xFF | ||
|
||
/** | ||
* Computes the sha2 hash for the string | ||
* | ||
* @param input the string for which sha2 hash is to be computed | ||
* @return sha2 hash result if the string is valid, null otherwise | ||
*/ | ||
@JvmStatic | ||
fun sha2hash(input: String?): String? { | ||
if (input.isNullOrEmpty()) { | ||
return null | ||
} | ||
|
||
try { | ||
val messageDigest = MessageDigest.getInstance("SHA-256").apply { | ||
update(input.toByteArray(StandardCharsets.UTF_8)) | ||
}.digest() | ||
|
||
val sha2HexBuilder = StringBuilder() | ||
for (aMessageDigest in messageDigest) { | ||
val hexString = String.format("%02x", LSB_8_MASK and aMessageDigest.toInt()) | ||
sha2HexBuilder.append(hexString) | ||
} | ||
return sha2HexBuilder.toString() | ||
} catch (e: NoSuchAlgorithmException) { | ||
Log.debug( | ||
CoreConstants.LOG_TAG, LOG_TAG, | ||
"Failed to create SHA-256 hash for input: '$input', Error: $e" | ||
) | ||
} | ||
|
||
return null | ||
} | ||
} |
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
Oops, something went wrong.