Skip to content

Commit c9e627d

Browse files
committed
Updated decrypt and lookupPublicKeys methods
1 parent ea5b846 commit c9e627d

File tree

7 files changed

+93
-165
lines changed

7 files changed

+93
-165
lines changed

ethree/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ apply plugin: 'maven'
99
apply plugin: 'org.jetbrains.dokka'
1010

1111
group 'com.virgilsecurity'
12-
version '0.1.1-alpha'
12+
version '0.2.0'
1313

1414
dependencies {
1515
// Inner dependencies

ethree/src/main/java/com/virgilsecurity/android/ethree/kotlin/interaction/EThree.kt

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,14 @@ class EThree
319319
*
320320
* Can be called only after [bootstrap] otherwise [NotBootstrappedException] exception will be thrown
321321
*/
322-
fun decrypt(base64String: String, publicKeys: List<PublicKey>? = null): String {
322+
@JvmOverloads fun decrypt(base64String: String, sendersKey: PublicKey? = null): String {
323323
checkIfBootstrappedOrThrow()
324324

325325
if (base64String.isBlank()) throw EmptyArgumentException("data")
326-
if (publicKeys?.isEmpty() == true) throw EmptyArgumentException("publicKeys")
327-
if (publicKeys?.contains(loadCurrentPublicKey()) == true)
328-
throw IllegalArgumentException("You should not include your own public key.")
326+
if (sendersKey == loadCurrentPublicKey())
327+
throw IllegalArgumentException("You should not provide your own public key.")
329328

330-
return String(decrypt(ConvertionUtils.base64ToBytes(base64String), publicKeys))
329+
return String(decrypt(ConvertionUtils.base64ToBytes(base64String), sendersKey))
331330
}
332331

333332
/**
@@ -337,22 +336,20 @@ class EThree
337336
*
338337
* Can be called only after [bootstrap] otherwise [NotBootstrappedException] exception will be thrown
339338
*/
340-
fun decrypt(data: ByteArray, publicKeys: List<PublicKey>? = null): ByteArray {
339+
@JvmOverloads fun decrypt(data: ByteArray, sendersKey: PublicKey? = null): ByteArray {
341340
checkIfBootstrappedOrThrow()
342341

343342
if (data.isEmpty()) throw EmptyArgumentException("data")
344-
if (publicKeys?.isEmpty() == true) throw EmptyArgumentException("publicKeys")
345-
if (publicKeys?.contains(loadCurrentPublicKey()) == true)
346-
throw IllegalArgumentException("You should not include your own public key.")
343+
if (sendersKey == loadCurrentPublicKey())
344+
throw IllegalArgumentException("You should not provide your own public key.")
347345

348-
return (publicKeys == null).let { isNull ->
346+
return (sendersKey == null).let { isNull ->
349347
(if (isNull) {
350348
listOf(loadCurrentPublicKey() as VirgilPublicKey)
351349
} else {
352-
publicKeys?.asSequence()?.filterIsInstance<VirgilPublicKey>()?.toMutableList()
353-
?.apply {
354-
add(loadCurrentPublicKey() as VirgilPublicKey)
355-
}
350+
mutableListOf(sendersKey as VirgilPublicKey).apply {
351+
add(loadCurrentPublicKey() as VirgilPublicKey)
352+
}
356353
})
357354
}.let { keys ->
358355
virgilCrypto.decryptThenVerify(
@@ -373,7 +370,7 @@ class EThree
373370
* Can be called only after [bootstrap] otherwise [NotBootstrappedException] exception will be returned
374371
*/
375372
fun lookupPublicKeys(identities: List<String>,
376-
onResultListener: OnResultListener<List<PublicKey>>) {
373+
onResultListener: OnResultListener<Map<String, PublicKey>>) {
377374
GlobalScope.launch {
378375
try {
379376
checkIfBootstrappedOrThrow()
@@ -393,14 +390,13 @@ class EThree
393390
identities.map {
394391
searchCardsAsync(it) to it
395392
}.map {
396-
it.first.await() to it.second
393+
it.second to it.first.await()
397394
}.map {
398-
if (it.first.isNotEmpty())
399-
it.first.last().publicKey
395+
if (it.second.isNotEmpty())
396+
it.first to it.second.last().publicKey
400397
else
401-
throw PublicKeyNotFoundException(
402-
it.second)
403-
}.run {
398+
throw PublicKeyNotFoundException(it.first)
399+
}.toMap().run {
404400
onResultListener.onSuccess(this)
405401
}
406402
} catch (throwable: Throwable) {

ethreeCoroutines/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ apply plugin: 'maven'
99
apply plugin: 'org.jetbrains.dokka'
1010

1111
group 'com.virgilsecurity'
12-
version '0.1.0-alpha'
12+
version '0.1.1'
1313

1414
dependencies {
1515
// Inner dependencies

ethreeCoroutines/src/main/java/com/virgilsecurity/android/ethreecoroutines/interaction/EThree.kt

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,14 @@ class EThree
304304
*
305305
* Can be called only after [bootstrap] otherwise [NotBootstrappedException] exception will be thrown
306306
*/
307-
@JvmOverloads fun decrypt(base64String: String, publicKeys: List<PublicKey>? = null): String {
307+
@JvmOverloads fun decrypt(base64String: String, sendersKey: PublicKey? = null): String {
308308
checkIfBootstrappedOrThrow()
309309

310310
if (base64String.isBlank()) throw EmptyArgumentException("data")
311-
if (publicKeys?.isEmpty() == true) throw EmptyArgumentException("publicKeys")
312-
if (publicKeys?.contains(loadCurrentPublicKey()) == true)
313-
throw IllegalArgumentException("You should not include your own public key.")
311+
if (sendersKey == loadCurrentPublicKey())
312+
throw IllegalArgumentException("You should not provide your own public key.")
314313

315-
return String(decrypt(ConvertionUtils.base64ToBytes(base64String), publicKeys))
314+
return String(decrypt(ConvertionUtils.base64ToBytes(base64String), sendersKey))
316315
}
317316

318317
/**
@@ -322,22 +321,20 @@ class EThree
322321
*
323322
* Can be called only after [bootstrap] otherwise [NotBootstrappedException] exception will be thrown
324323
*/
325-
@JvmOverloads fun decrypt(data: ByteArray, publicKeys: List<PublicKey>? = null): ByteArray {
324+
@JvmOverloads fun decrypt(data: ByteArray, sendersKey: PublicKey? = null): ByteArray {
326325
checkIfBootstrappedOrThrow()
327326

328327
if (data.isEmpty()) throw EmptyArgumentException("data")
329-
if (publicKeys?.isEmpty() == true) throw EmptyArgumentException("publicKeys")
330-
if (publicKeys?.contains(loadCurrentPublicKey()) == true)
331-
throw IllegalArgumentException("You should not include your own public key.")
328+
if (sendersKey == loadCurrentPublicKey())
329+
throw IllegalArgumentException("You should not provide your own public key.")
332330

333-
return (publicKeys == null).let { isNull ->
331+
return (sendersKey == null).let { isNull ->
334332
(if (isNull) {
335333
listOf(loadCurrentPublicKey() as VirgilPublicKey)
336334
} else {
337-
publicKeys?.asSequence()?.filterIsInstance<VirgilPublicKey>()?.toMutableList()
338-
?.apply {
339-
add(loadCurrentPublicKey() as VirgilPublicKey)
340-
}
335+
mutableListOf(sendersKey as VirgilPublicKey).apply {
336+
add(loadCurrentPublicKey() as VirgilPublicKey)
337+
}
341338
})
342339
}.let { keys ->
343340
virgilCrypto.decryptThenVerify(
@@ -357,26 +354,29 @@ class EThree
357354
*
358355
* Can be called only after [bootstrap] otherwise [NotBootstrappedException] exception will be returned
359356
*/
360-
fun lookupPublicKeys(identities: List<String>): Deferred<List<PublicKey>> = GlobalScope.async {
361-
checkIfBootstrappedOrThrow()
362-
357+
fun lookupPublicKeys(identities: List<String>): Deferred<Map<String, PublicKey>> = GlobalScope.async {
363358
if (identities.isEmpty()) throw EmptyArgumentException("identities")
364-
identities.groupingBy { it }.eachCount().filter { it.value > 1 }.run {
365-
if (this.isNotEmpty())
366-
throw PublicKeyDuplicateException("Duplicates are not allowed. " +
367-
"Duplicated identities:\n${this}")
368-
}
359+
identities.groupingBy { it }
360+
.eachCount()
361+
.filter { it.value > 1 }
362+
.run {
363+
if (this.isNotEmpty())
364+
throw PublicKeyDuplicateException(
365+
"Duplicates are not allowed. " +
366+
"Duplicated identities:\n${this}"
367+
)
368+
}
369369

370370
identities.map {
371371
cardManager.searchCards(it) to it
372372
}.map {
373-
it.first to it.second
373+
it.second to it.first
374374
}.map {
375-
if (it.first.isNotEmpty())
376-
it.first.last().publicKey
375+
if (it.second.isNotEmpty())
376+
it.first to it.second.last().publicKey
377377
else
378-
throw PublicKeyNotFoundException(it.second)
379-
}
378+
throw PublicKeyNotFoundException(it.first)
379+
}.toMap()
380380
}
381381

382382
/**

tests/src/androidTest/java/com/virgilsecurity/android/ethree/kotlin/interaction/EThreeEncryptionTest.kt

Lines changed: 23 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ import java.util.concurrent.TimeUnit
6666
class EThreeEncryptionTest {
6767

6868
private val identity = UUID.randomUUID().toString()
69-
private val password = UUID.randomUUID().toString()
7069

7170
private lateinit var eThree: EThree
7271
private lateinit var jwtGenerator: JwtGenerator
@@ -161,10 +160,10 @@ class EThreeEncryptionTest {
161160
cardManagerOne).right)
162161

163162
eThree.lookupPublicKeys(listOf(identityOne),
164-
object : EThree.OnResultListener<List<PublicKey>> {
165-
override fun onSuccess(result: List<PublicKey>) {
163+
object : EThree.OnResultListener<Map<String, PublicKey>> {
164+
override fun onSuccess(result: Map<String, PublicKey>) {
166165
assertTrue(result.isNotEmpty() && result.size == 1)
167-
assertEquals(publishedCardOne.publicKey, result[0])
166+
assertEquals(publishedCardOne.publicKey, result[identityOne])
168167
}
169168

170169
override fun onError(throwable: Throwable) {
@@ -176,7 +175,7 @@ class EThreeEncryptionTest {
176175
// STE-Encrypt-1
177176
@Test
178177
fun lookup_multiply_users() {
179-
var foundCards = 0
178+
var foundCards = false
180179

181180
// Card one
182181
val identityOne = UUID.randomUUID().toString()
@@ -195,21 +194,17 @@ class EThreeEncryptionTest {
195194
cardManagerThree).right)
196195

197196
eThree.lookupPublicKeys(listOf(identityOne, identityTwo, identityThree),
198-
object : EThree.OnResultListener<List<PublicKey>> {
197+
object : EThree.OnResultListener<Map<String, PublicKey>> {
199198

200-
override fun onSuccess(result: List<PublicKey>) {
199+
override fun onSuccess(result: Map<String, PublicKey>) {
201200
assertTrue(result.isNotEmpty() && result.size == 3)
202-
for (key in result) {
203-
result.find {
204-
(it == publishedCardOne.publicKey
205-
|| it == publishedCardTwo.publicKey
206-
|| it == publishedCardThree.publicKey)
207-
}.run {
208-
foundCards++
201+
if (result[identityOne] == publishedCardOne.publicKey
202+
&& result[identityTwo] == publishedCardTwo.publicKey
203+
&& result[identityThree] == publishedCardThree.publicKey) {
204+
foundCards= true
209205
}
210-
}
211206

212-
assertTrue(foundCards == 3)
207+
assertTrue(foundCards)
213208
}
214209

215210
override fun onError(throwable: Throwable) {
@@ -221,8 +216,8 @@ class EThreeEncryptionTest {
221216
//STE-Encrypt-2
222217
@Test
223218
fun lookup_zero_users() {
224-
eThree.lookupPublicKeys(listOf(), object : EThree.OnResultListener<List<PublicKey>> {
225-
override fun onSuccess(result: List<PublicKey>) {
219+
eThree.lookupPublicKeys(listOf(), object : EThree.OnResultListener<Map<String, PublicKey>> {
220+
override fun onSuccess(result: Map<String, PublicKey>) {
226221
fail("Illegal State")
227222
}
228223

@@ -241,9 +236,9 @@ class EThreeEncryptionTest {
241236

242237
val waiter = CountDownLatch(1)
243238
eThree.lookupPublicKeys(listOf(identity, identityTwo),
244-
object : EThree.OnResultListener<List<PublicKey>> {
245-
override fun onSuccess(result: List<PublicKey>) {
246-
eThreeKeys.addAll(result)
239+
object : EThree.OnResultListener<Map<String, PublicKey>> {
240+
override fun onSuccess(result: Map<String, PublicKey>) {
241+
eThreeKeys.addAll(result.values.toList())
247242
waiter.countDown()
248243
}
249244

@@ -273,9 +268,9 @@ class EThreeEncryptionTest {
273268

274269
val waiter = CountDownLatch(1)
275270
eThree.lookupPublicKeys(listOf(identity, identityTwo),
276-
object : EThree.OnResultListener<List<PublicKey>> {
277-
override fun onSuccess(result: List<PublicKey>) {
278-
eThreeKeys.addAll(result)
271+
object : EThree.OnResultListener<Map<String, PublicKey>> {
272+
override fun onSuccess(result: Map<String, PublicKey>) {
273+
eThreeKeys.addAll(result.values.toList())
279274
waiter.countDown()
280275
}
281276

@@ -291,14 +286,13 @@ class EThreeEncryptionTest {
291286
val wrongPublicKey = TestConfig.virgilCrypto.generateKeys().publicKey
292287
var failedWithWrongKey = false
293288
try {
294-
eThreeTwo.decrypt(encryptedForOne, listOf(wrongPublicKey))
289+
eThreeTwo.decrypt(encryptedForOne, wrongPublicKey)
295290
} catch (throwable: Throwable) {
296291
failedWithWrongKey = true
297292
}
298293
assertTrue(failedWithWrongKey)
299294

300-
val decryptedByTwo = eThreeTwo.decrypt(encryptedForOne,
301-
listOf(wrongPublicKey, eThreeKeys[0]))
295+
val decryptedByTwo = eThreeTwo.decrypt(encryptedForOne, eThreeKeys[0])
302296

303297
assertEquals(RAW_TEXT, decryptedByTwo)
304298
}
@@ -309,40 +303,6 @@ class EThreeEncryptionTest {
309303
eThree.encrypt(RAW_TEXT, listOf())
310304
}
311305

312-
// STE-Encrypt-5
313-
@Test
314-
fun decrypt_for_zero_users() {
315-
val identityTwo = UUID.randomUUID().toString()
316-
initAndBootstrapEThree(identityTwo)
317-
318-
var eThreeKey: PublicKey? = null
319-
320-
val waiter = CountDownLatch(1)
321-
eThree.lookupPublicKeys(listOf(identityTwo),
322-
object : EThree.OnResultListener<List<PublicKey>> {
323-
override fun onSuccess(result: List<PublicKey>) {
324-
eThreeKey = result.last()
325-
waiter.countDown()
326-
}
327-
328-
override fun onError(throwable: Throwable) {
329-
fail(throwable.message)
330-
}
331-
})
332-
waiter.await(TestUtils.THROTTLE_TIMEOUT, TimeUnit.SECONDS)
333-
334-
assertNotNull(eThreeKey)
335-
val encryptedForOne = eThree.encrypt(RAW_TEXT, listOf(eThreeKey!!))
336-
337-
var failedDecrypt = false
338-
try {
339-
eThree.decrypt(encryptedForOne, listOf())
340-
} catch (e: EmptyArgumentException) {
341-
failedDecrypt = true
342-
}
343-
assertTrue(failedDecrypt)
344-
}
345-
346306
// STE-Encrypt-6
347307
@Test
348308
fun encrypt_decrypt_for_owner() {
@@ -361,7 +321,7 @@ class EThreeEncryptionTest {
361321

362322
var failedDecrypt = false
363323
try {
364-
eThree.decrypt(encryptedWithoutSign, listOf(keyPair.publicKey))
324+
eThree.decrypt(encryptedWithoutSign, keyPair.publicKey)
365325
} catch (e: Exception) {
366326
failedDecrypt = true
367327
}
@@ -385,7 +345,7 @@ class EThreeEncryptionTest {
385345

386346
var failedDecrypt = false
387347
try {
388-
eThreeTwo.decrypt(RAW_TEXT, listOf(anyKeypair.publicKey))
348+
eThreeTwo.decrypt(RAW_TEXT, anyKeypair.publicKey)
389349
} catch (e: NotBootstrappedException) {
390350
failedDecrypt = true
391351
}

tests/src/androidTest/java/com/virgilsecurity/android/ethree/kotlin/interaction/EThreeNegativeTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ class EThreeNegativeTest {
210210
fun lookup_fail_without_bootstrap() {
211211
var failed = false
212212
val waiter = CountDownLatch(1)
213-
eThree.lookupPublicKeys(listOf(""), object : EThree.OnResultListener<List<PublicKey>> {
214-
override fun onSuccess(result: List<PublicKey>) {
213+
eThree.lookupPublicKeys(listOf(""), object : EThree.OnResultListener<Map<String, PublicKey>> {
214+
override fun onSuccess(result: Map<String, PublicKey>) {
215215
fail("Not Bootstrapped")
216216
}
217217

@@ -231,8 +231,8 @@ class EThreeNegativeTest {
231231
var failed = false
232232
val waiter = CountDownLatch(1)
233233
eThree.lookupPublicKeys(listOf(identity, WRONG_IDENTITY),
234-
object : EThree.OnResultListener<List<PublicKey>> {
235-
override fun onSuccess(result: List<PublicKey>) {
234+
object : EThree.OnResultListener<Map<String, PublicKey>> {
235+
override fun onSuccess(result: Map<String, PublicKey>) {
236236
fail("Illegal State")
237237
}
238238

@@ -277,8 +277,8 @@ class EThreeNegativeTest {
277277
eThree.lookupPublicKeys(listOf(identity, identity, identity,
278278
WRONG_IDENTITY, WRONG_IDENTITY,
279279
WRONG_IDENTITY + identity),
280-
object : EThree.OnResultListener<List<PublicKey>> {
281-
override fun onSuccess(result: List<PublicKey>) {
280+
object : EThree.OnResultListener<Map<String, PublicKey>> {
281+
override fun onSuccess(result: Map<String, PublicKey>) {
282282
fail("Illegal State")
283283
}
284284

0 commit comments

Comments
 (0)