Skip to content

Commit 649ea7f

Browse files
author
Utngy Pisal
committed
Change hash function
1 parent fe0b67a commit 649ea7f

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hybrid-crypto/src/main/java/me/pisal/hybridcrypto/aes/AESCipher.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,20 @@ import javax.crypto.spec.PBEKeySpec
1313
import javax.crypto.spec.SecretKeySpec
1414

1515
class AESCipher(
16+
/**
17+
* Recommended by OWASP: 16 (bytes)
18+
*/
1619
private val aesKeySizeInBytes: Int,
20+
21+
/**
22+
* Recommended by OWASP: "AES/CBC/PKCS7Padding"
23+
*/
1724
private val aesMode: String = "AES/CBC/PKCS7Padding",
25+
26+
/**
27+
* Recommended: 100 good an equilibrium between robustness and performance.
28+
* The bigger, the more secure, but slower at the same time.
29+
*/
1830
private val keyIterationCount: Int = 100
1931
) {
2032

hybrid-crypto/src/main/java/me/pisal/hybridcrypto/hybrid/HybridCrypto.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import me.pisal.hybridcrypto.rsa.RSACipher
66
import java.io.Serializable
77
import java.security.MessageDigest
88
import java.security.SecureRandom
9-
import kotlin.experimental.and
109

1110
/**
1211
* A combined crypto mechanism of
@@ -67,15 +66,24 @@ class HybridCrypto private constructor() {
6766
}
6867

6968
private fun sha512(message: String): String {
69+
val bytes = message.toByteArray()
7070
val md = MessageDigest.getInstance("SHA-512")
71-
val digest = md.digest(message.toByteArray())
72-
val builder = StringBuilder()
73-
for (i in digest.indices) {
74-
builder.append(((digest[i] and 0xff.toByte()) + 0x100)
75-
.toString(16)
76-
.substring(1))
77-
}
78-
return builder.toString()
71+
val digest = md.digest(bytes)
72+
return digest.fold("") { str, it -> str + "%02x".format(it) }
73+
74+
// val bytes = input.toByteArray()
75+
// val md = MessageDigest.getInstance("SHA-512")
76+
// val digest = md.digest(bytes)
77+
// return digest.fold("") { str, it -> str + "%02x".format(it) }
78+
// val md = MessageDigest.getInstance("SHA-512")
79+
// val digest = md.digest(message.toByteArray())
80+
// val builder = StringBuilder()
81+
// for (i in digest.indices) {
82+
// builder.append(((digest[i] and 0xff.toByte()) + 0x100)
83+
// .toString(16)
84+
// .substring(1))
85+
// }
86+
// return builder.toString()
7987
}
8088

8189
data class Configuration(

0 commit comments

Comments
 (0)