-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to use file checksum as filename by #1555
- Loading branch information
Showing
23 changed files
with
384 additions
and
14 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
133 changes: 133 additions & 0 deletions
133
core/data/src/main/java/ru/tech/imageresizershrinker/core/data/utils/ChecksumUtils.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,133 @@ | ||
/* | ||
* ImageToolbox is an image editor for android | ||
* Copyright (c) 2024 T8RIN (Malik Mukhametzyanov) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* | ||
* 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 CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* You should have received a copy of the Apache License | ||
* along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
|
||
package ru.tech.imageresizershrinker.core.data.utils | ||
|
||
import ru.tech.imageresizershrinker.core.domain.model.ChecksumType | ||
import java.io.File | ||
import java.io.InputStream | ||
import java.security.MessageDigest | ||
|
||
private const val STREAM_BUFFER_LENGTH = 1024 | ||
|
||
internal fun ChecksumType.computeFromFile( | ||
filePath: String | ||
): String = computeFromFile(File(filePath)) | ||
|
||
internal fun ChecksumType.computeFromFile( | ||
file: File | ||
): String = file.inputStream().use { | ||
computeFromInputStream(it) | ||
} | ||
|
||
internal fun ChecksumType.computeFromByteArray( | ||
byteArray: ByteArray | ||
): String = byteArray.inputStream().use { | ||
computeFromInputStream(it) | ||
} | ||
|
||
internal fun ChecksumType.computeFromInputStream( | ||
inputStream: InputStream | ||
): String { | ||
val byteArray = updateDigest(inputStream).digest() | ||
val hexCode = encodeHex(byteArray, true) | ||
return String(hexCode) | ||
} | ||
|
||
/** | ||
* Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. | ||
* The returned array will be double the length of the passed array, as it takes two characters to represent any | ||
* given byte. | ||
* | ||
* @param data a byte[] to convert to Hex characters | ||
* @param toLowerCase `true` converts to lowercase, `false` to uppercase | ||
* @return A char[] containing hexadecimal characters in the selected case | ||
*/ | ||
internal fun encodeHex( | ||
data: ByteArray, | ||
toLowerCase: Boolean | ||
): CharArray = encodeHex( | ||
data = data, | ||
toDigits = if (toLowerCase) { | ||
DIGITS_LOWER | ||
} else { | ||
DIGITS_UPPER | ||
} | ||
) | ||
|
||
/** | ||
* Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. | ||
* The returned array will be double the length of the passed array, as it takes two characters to represent any | ||
* given byte. | ||
* | ||
* @param data a byte[] to convert to Hex characters | ||
* @param toDigits the output alphabet (must contain at least 16 chars) | ||
* @return A char[] containing the appropriate characters from the alphabet | ||
* For best results, this should be either upper- or lower-case hex. | ||
*/ | ||
internal fun encodeHex( | ||
data: ByteArray, | ||
toDigits: CharArray | ||
): CharArray { | ||
val l = data.size | ||
val out = CharArray(l shl 1) | ||
// two characters form the hex value. | ||
var i = 0 | ||
var j = 0 | ||
while (i < l) { | ||
out[j++] = toDigits[0xF0 and data[i].toInt() ushr 4] | ||
out[j++] = toDigits[0x0F and data[i].toInt()] | ||
i++ | ||
} | ||
return out | ||
} | ||
|
||
/** | ||
* Reads through an InputStream and updates the digest for the data | ||
* | ||
* @param ChecksumType The ChecksumType to use (e.g. MD5) | ||
* @param data Data to digest | ||
* @return the digest | ||
*/ | ||
private fun ChecksumType.updateDigest( | ||
data: InputStream | ||
): MessageDigest { | ||
val digest = toDigest() | ||
|
||
val buffer = ByteArray(STREAM_BUFFER_LENGTH) | ||
var read = data.read(buffer, 0, STREAM_BUFFER_LENGTH) | ||
while (read > -1) { | ||
digest.update(buffer, 0, read) | ||
read = data.read(buffer, 0, STREAM_BUFFER_LENGTH) | ||
} | ||
return digest | ||
} | ||
|
||
/** | ||
* Used to build output as Hex | ||
*/ | ||
private val DIGITS_LOWER = | ||
charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f') | ||
|
||
/** | ||
* Used to build output as Hex | ||
*/ | ||
private val DIGITS_UPPER = | ||
charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F') | ||
|
||
|
||
private fun ChecksumType.toDigest(): MessageDigest = MessageDigest.getInstance(digest) |
67 changes: 67 additions & 0 deletions
67
core/domain/src/main/kotlin/ru/tech/imageresizershrinker/core/domain/model/ChecksumType.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,67 @@ | ||
/* | ||
* ImageToolbox is an image editor for android | ||
* Copyright (c) 2024 T8RIN (Malik Mukhametzyanov) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* | ||
* 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 CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* You should have received a copy of the Apache License | ||
* along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
|
||
package ru.tech.imageresizershrinker.core.domain.model | ||
|
||
class ChecksumType private constructor( | ||
val digest: String | ||
) { | ||
override fun toString(): String = "ChecksumType($digest)" | ||
|
||
companion object { | ||
val MD2 = ChecksumType("MD2") | ||
val MD5 = ChecksumType("MD5") | ||
val SHA_1 = ChecksumType("SHA-1") | ||
val SHA_224 = ChecksumType("SHA-224") | ||
val SHA_256 = ChecksumType("SHA-256") | ||
val SHA_384 = ChecksumType("SHA-384") | ||
val SHA_512 = ChecksumType("SHA-512") | ||
// Not supported by Android | ||
// val SHA_512_224 = ChecksumType("SHA-512/224") | ||
// val SHA_512_256 = ChecksumType("SHA-512/256") | ||
// val SHA3_224 = ChecksumType("SHA3-224") | ||
// val SHA3_256 = ChecksumType("SHA3-256") | ||
// val SHA3_384 = ChecksumType("SHA3-384") | ||
// val SHA3_512 = ChecksumType("SHA3-512") | ||
|
||
val entries: List<ChecksumType> by lazy { | ||
listOf( | ||
MD2, | ||
MD5, | ||
SHA_1, | ||
SHA_224, | ||
SHA_256, | ||
SHA_384, | ||
SHA_512, | ||
// SHA_512_224, | ||
// SHA_512_256, | ||
// SHA3_224, | ||
// SHA3_256, | ||
// SHA3_384, | ||
// SHA3_512, | ||
) | ||
} | ||
|
||
fun fromString( | ||
digest: String? | ||
): ChecksumType? = digest?.let { | ||
entries.find { | ||
it.digest == digest | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.