summaryrefslogtreecommitdiff
path: root/util/src/main/kotlin/strings.kt
blob: dce25861bc467b721b1ba24b408538427321c19b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * This file is part of LibEuFin.
 * Copyright (C) 2020 Taler Systems S.A.
 *
 * LibEuFin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3, or
 * (at your option) any later version.
 *
 * LibEuFin is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
 * Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with LibEuFin; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>
 */

package tech.libeufin.util

import UtilError
import io.ktor.http.HttpStatusCode
import java.math.BigInteger
import java.math.BigDecimal
import java.util.*

fun ByteArray.toHexString(): String {
    return this.joinToString("") {
        java.lang.String.format("%02X", it)
    }
}

private fun toDigit(hexChar: Char): Int {
    val digit = Character.digit(hexChar, 16)
    require(digit != -1) { "Invalid Hexadecimal Character: $hexChar" }
    return digit
}

private fun hexToByte(hexString: String): Byte {
    val firstDigit: Int = toDigit(hexString[0])
    val secondDigit: Int = toDigit(hexString[1])
    return ((firstDigit shl 4) + secondDigit).toByte()
}

fun decodeHexString(hexString: String): ByteArray {
    val hs = hexString.replace(" ", "").replace("\n", "")
    require(hs.length % 2 != 1) { "Invalid hexadecimal String supplied." }
    val bytes = ByteArray(hs.length / 2)
    var i = 0
    while (i < hs.length) {
        bytes[i / 2] = hexToByte(hs.substring(i, i + 2))
        i += 2
    }
    return bytes
}

fun bytesToBase64(bytes: ByteArray): String {
    return Base64.getEncoder().encodeToString(bytes)
}

fun base64ToBytes(encoding: String): ByteArray {
    return Base64.getDecoder().decode(encoding)
}

// used mostly in RSA math, never as amount.
fun BigInteger.toUnsignedHexString(): String {
    val signedValue = this.toByteArray()
    require(this.signum() > 0) { "number must be positive" }
    val start = if (signedValue[0] == 0.toByte()) {
        1
    } else {
        0
    }
    val bytes = Arrays.copyOfRange(signedValue, start, signedValue.size)
    return bytes.toHexString()
}

/**
 * Inserts spaces every 2 characters, and a newline after 8 pairs.
 */
fun chunkString(input: String): String {
    val ret = StringBuilder()
    var columns = 0
    for (i in input.indices) {
        if ((i + 1).rem(2) == 0) {
            if (columns == 15) {
                ret.append(input[i] + "\n")
                columns = 0
                continue
            }
            ret.append(input[i] + " ")
            columns++
            continue
        }
        ret.append(input[i])
    }
    return ret.toString().uppercase()
}

data class AmountWithCurrency(
    val currency: String,
    val amount: String
)

fun parseDecimal(decimalStr: String): BigDecimal {
    if(!validatePlainAmount(decimalStr, withSign = true))
        throw UtilError(
            HttpStatusCode.BadRequest,
            "Bad string amount given: $decimalStr",
            LibeufinErrorCode.LIBEUFIN_EC_GENERIC_PARAMETER_MALFORMED
        )
    return try {
        BigDecimal(decimalStr)
    } catch (e: NumberFormatException) {
        throw UtilError(
            HttpStatusCode.BadRequest,
            "Bad string amount given: $decimalStr",
            LibeufinErrorCode.LIBEUFIN_EC_GENERIC_PARAMETER_MALFORMED
        )
    }
}

fun getRandomString(length: Int): String {
    val allowedChars = ('A' .. 'Z') + ('0' .. '9')
    return (1 .. length)
        .map { allowedChars.random() }
        .joinToString("")
}

// Taken from the ISO20022 XSD schema
private val bicRegex = Regex("^[A-Z]{6}[A-Z2-9][A-NP-Z0-9]([A-Z0-9]{3})?$")

fun validateBic(bic: String): Boolean {
    return bicRegex.matches(bic)
}

// Taken from the ISO20022 XSD schema
private val ibanRegex = Regex("^[A-Z]{2}[0-9]{2}[a-zA-Z0-9]{1,30}$")

fun validateIban(iban: String): Boolean {
    return ibanRegex.matches(iban)
}

fun isValidResourceName(name: String): Boolean {
    return name.matches(Regex("[a-z]([-a-z0-9]*[a-z0-9])?"))
}

fun requireValidResourceName(name: String): String {
    if (!isValidResourceName(name)) {
        throw UtilError(
            HttpStatusCode.BadRequest,
            "Invalid resource name. The first character must be a lowercase letter, " +
                    "and all following characters (except for the last character) must be a dash, " +
                    "lowercase letter, or digit. The last character must be a lowercase letter or digit.",
            LibeufinErrorCode.LIBEUFIN_EC_GENERIC_PARAMETER_MALFORMED
        )
    }
    return name
}


fun sanityCheckOrThrow(credentials: Pair<String, String>) {
    if (!sanityCheckCredentials(credentials)) throw UtilError(
        HttpStatusCode.BadRequest,
        "Please only use alphanumeric credentials.",
        LibeufinErrorCode.LIBEUFIN_EC_GENERIC_PARAMETER_MALFORMED
    )
}

/**
 * Sanity-check user's credentials.
 */
fun sanityCheckCredentials(credentials: Pair<String, String>): Boolean {
    val allowedChars = Regex("^[a-zA-Z0-9]+$")
    if (!allowedChars.matches(credentials.first)) return false
    if (!allowedChars.matches(credentials.second)) return false
    return true
}

/**
 * Parses string into java.util.UUID format or throws 400 Bad Request.
 * The output is usually consumed in database queries.
 */
fun parseUuid(maybeUuid: String): UUID {
    val uuid = try {
        UUID.fromString(maybeUuid)
    } catch (e: Exception) {
        throw badRequest("'$maybeUuid' is an invalid UUID.")
    }
    return uuid
}

fun hasWopidPlaceholder(captchaUrl: String): Boolean {
    if (captchaUrl.contains("{wopid}", ignoreCase = true))
        return true
    return false
}

// Tries to extract a valid reserve public key from the raw subject line
fun extractReservePubFromSubject(rawSubject: String): String? {
    val re = "\\b[a-z0-9A-Z]{52}\\b".toRegex()
    val result = re.find(rawSubject.replace("[\n]+".toRegex(), "")) ?: return null
    return result.value.uppercase()
}