From 7c73d2418cd357b1e5d74688754ffcd5a433bd65 Mon Sep 17 00:00:00 2001 From: Alexander Buntakov Date: Wed, 11 Aug 2021 20:12:37 +0300 Subject: [PATCH] add secure methods (#44) --- .../src/main/kotlin/ru/touchin/common/byte/ByteUtils.kt | 8 ++++++++ .../touchin/common/random/SecureRandomStringGenerator.kt | 2 ++ 2 files changed, 10 insertions(+) diff --git a/common/src/main/kotlin/ru/touchin/common/byte/ByteUtils.kt b/common/src/main/kotlin/ru/touchin/common/byte/ByteUtils.kt index 3a7e7fd..d126482 100644 --- a/common/src/main/kotlin/ru/touchin/common/byte/ByteUtils.kt +++ b/common/src/main/kotlin/ru/touchin/common/byte/ByteUtils.kt @@ -8,4 +8,12 @@ object ByteUtils { } } + fun decodeHex(hex: String): ByteArray { + check(hex.length % 2 == 0) { "Must have an even length" } + + return hex.chunked(2) + .map { it.toInt(16).toByte() } + .toByteArray() + } + } diff --git a/common/src/main/kotlin/ru/touchin/common/random/SecureRandomStringGenerator.kt b/common/src/main/kotlin/ru/touchin/common/random/SecureRandomStringGenerator.kt index 8fb1266..e5843ae 100644 --- a/common/src/main/kotlin/ru/touchin/common/random/SecureRandomStringGenerator.kt +++ b/common/src/main/kotlin/ru/touchin/common/random/SecureRandomStringGenerator.kt @@ -24,4 +24,6 @@ object SecureRandomStringGenerator { } } + fun generateRandomBytes(size: Int) = ByteArray(size).also(SecureRandom()::nextBytes) + }