This commit is contained in:
Vitor Pamplona
2026-04-16 08:51:15 -04:00
parent 42cd1cdf0c
commit fb9171fb9a
7 changed files with 55 additions and 35 deletions
@@ -99,12 +99,13 @@ fun buildMip04IMetaTag(
dimensions: String? = null,
blurhash: String? = null,
): IMetaTag =
IMetaTagBuilder(url).apply {
add(Mip04Fields.MIME_TYPE, Mip04MediaEncryption.canonicalizeMimeType(mimeType))
add(Mip04Fields.FILENAME, filename)
add(Mip04Fields.FILE_HASH, originalFileHash.toHexKey())
add(Mip04Fields.NONCE, nonce.toHexKey())
add(Mip04Fields.VERSION, Mip04MediaEncryption.VERSION)
dimensions?.let { add(Mip04Fields.DIMENSIONS, it) }
blurhash?.let { add(Mip04Fields.BLURHASH, it) }
}.build()
IMetaTagBuilder(url)
.apply {
add(Mip04Fields.MIME_TYPE, Mip04MediaEncryption.canonicalizeMimeType(mimeType))
add(Mip04Fields.FILENAME, filename)
add(Mip04Fields.FILE_HASH, originalFileHash.toHexKey())
add(Mip04Fields.NONCE, nonce.toHexKey())
add(Mip04Fields.VERSION, Mip04MediaEncryption.VERSION)
dimensions?.let { add(Mip04Fields.DIMENSIONS, it) }
blurhash?.let { add(Mip04Fields.BLURHASH, it) }
}.build()
@@ -161,14 +161,22 @@ object Mip04MediaEncryption {
val result = ByteArray(size)
var offset = 0
SCHEME_BYTES.copyInto(result, offset); offset += SCHEME_BYTES.size
NULL_SEPARATOR.copyInto(result, offset); offset += 1
fileHash.copyInto(result, offset); offset += fileHash.size
NULL_SEPARATOR.copyInto(result, offset); offset += 1
mimeBytes.copyInto(result, offset); offset += mimeBytes.size
NULL_SEPARATOR.copyInto(result, offset); offset += 1
filenameBytes.copyInto(result, offset); offset += filenameBytes.size
NULL_SEPARATOR.copyInto(result, offset); offset += 1
SCHEME_BYTES.copyInto(result, offset)
offset += SCHEME_BYTES.size
NULL_SEPARATOR.copyInto(result, offset)
offset += 1
fileHash.copyInto(result, offset)
offset += fileHash.size
NULL_SEPARATOR.copyInto(result, offset)
offset += 1
mimeBytes.copyInto(result, offset)
offset += mimeBytes.size
NULL_SEPARATOR.copyInto(result, offset)
offset += 1
filenameBytes.copyInto(result, offset)
offset += filenameBytes.size
NULL_SEPARATOR.copyInto(result, offset)
offset += 1
suffix.copyInto(result, offset)
return result
@@ -190,12 +198,18 @@ object Mip04MediaEncryption {
val result = ByteArray(size)
var offset = 0
SCHEME_BYTES.copyInto(result, offset); offset += SCHEME_BYTES.size
NULL_SEPARATOR.copyInto(result, offset); offset += 1
fileHash.copyInto(result, offset); offset += fileHash.size
NULL_SEPARATOR.copyInto(result, offset); offset += 1
mimeBytes.copyInto(result, offset); offset += mimeBytes.size
NULL_SEPARATOR.copyInto(result, offset); offset += 1
SCHEME_BYTES.copyInto(result, offset)
offset += SCHEME_BYTES.size
NULL_SEPARATOR.copyInto(result, offset)
offset += 1
fileHash.copyInto(result, offset)
offset += fileHash.size
NULL_SEPARATOR.copyInto(result, offset)
offset += 1
mimeBytes.copyInto(result, offset)
offset += mimeBytes.size
NULL_SEPARATOR.copyInto(result, offset)
offset += 1
filenameBytes.copyInto(result, offset)
return result
@@ -208,7 +222,11 @@ object Mip04MediaEncryption {
* - Strip parameters (e.g., "; charset=utf-8")
*/
fun canonicalizeMimeType(mimeType: String): String =
mimeType.trim().lowercase().substringBefore(";").trim()
mimeType
.trim()
.lowercase()
.substringBefore(";")
.trim()
}
class Mip04EncryptionResult(