DocumentsStorageProvider: hash the accountName for rootId map

This should handle the fact that accountNames are of arbitrary length

MD5 is fine for this, as it should be quicker than SHA and the chance of collisions here is very low

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey 2022-05-06 16:34:15 +02:00
parent 3b3d23ff0a
commit 02f23f326d
No known key found for this signature in database
GPG key ID: 2585783189A62105
3 changed files with 97 additions and 8 deletions

View file

@ -0,0 +1,42 @@
/*
* Nextcloud Android Library is available under MIT license
*
* @author Álvaro Brey Vilas
* Copyright (C) 2022 Álvaro Brey Vilas
* Copyright (C) 2022 Nextcloud GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.nextcloud.client.utils
import org.apache.commons.codec.binary.Hex
import java.security.MessageDigest
object HashUtil {
private const val ALGORITHM_MD5 = "MD5"
@JvmStatic
fun md5Hash(input: String): String {
val digest = MessageDigest.getInstance(ALGORITHM_MD5)
.digest(input.toByteArray())
return String(Hex.encodeHex(digest))
}
}

View file

@ -47,6 +47,7 @@ import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.files.downloader.DownloadTask; import com.nextcloud.client.files.downloader.DownloadTask;
import com.nextcloud.client.preferences.AppPreferences; import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.client.preferences.AppPreferencesImpl; import com.nextcloud.client.preferences.AppPreferencesImpl;
import com.nextcloud.client.utils.HashUtil;
import com.owncloud.android.MainApp; import com.owncloud.android.MainApp;
import com.owncloud.android.R; import com.owncloud.android.R;
import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.FileDataStorageManager;
@ -80,8 +81,6 @@ import org.nextcloud.providers.cursors.RootCursor;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -685,12 +684,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
} }
private String rootIdForUser(User user) { private String rootIdForUser(User user) {
try { return HashUtil.md5Hash(user.getAccountName());
return URLEncoder.encode(user.getAccountName(), "UTF-8");
} catch (UnsupportedEncodingException e) {
// this is a theoretical exception as UTF-8 is a standard required encoding
throw new IllegalStateException("UTF-8 encoding unavailable in this environment", e);
}
} }
private void initiateStorageMap() { private void initiateStorageMap() {

View file

@ -0,0 +1,53 @@
/*
* Nextcloud Android Library is available under MIT license
*
* @author Álvaro Brey Vilas
* Copyright (C) 2022 Álvaro Brey Vilas
* Copyright (C) 2022 Nextcloud GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.nextcloud.client.utils
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
@RunWith(Parameterized::class)
class HashUtilTest(private val input: String, private val expected: String) {
companion object {
@JvmStatic
@Parameterized.Parameters(name = "{0}")
fun params(): List<Array<Any>> = listOf(
arrayOf("", "d41d8cd98f00b204e9800998ecf8427e"),
arrayOf("test", "098f6bcd4621d373cade4e832627b4f6"),
arrayOf("test@nextcloud.localhost", "12aa338095d171f307c3e3f724702ab1"),
arrayOf("tost@nextcloud.localhost", "e01e5301f90c123a65e872d68e84c4b2")
)
}
@Test
fun testMd5Hash() {
val hash = HashUtil.md5Hash(input)
Assert.assertEquals("Wrong hash for input", expected, hash)
}
}