blob: 79c78a2955edf48d923fc3d3d7d11f5741abed78 (
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
|
/*
* This file is part of LibEuFin.
* Copyright (C) 2024 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.ebics
import tech.libeufin.common.deflate
import tech.libeufin.common.inflate
import tech.libeufin.common.toHexString
import java.security.SecureRandom
/**
* Helpers for dealing with order compression, encryption, decryption, chunking and re-assembly.
*/
object EbicsOrderUtil {
inline fun <reified T> decodeOrderDataXml(encodedOrderData: ByteArray): T {
return encodedOrderData.inputStream().inflate().use {
XMLUtil.convertToJaxb<T>(it).value
}
}
inline fun <reified T> encodeOrderDataXml(obj: T): ByteArray {
val bytes = XMLUtil.convertJaxbToBytes(obj)
return bytes.inputStream().deflate().readAllBytes()
}
@ExperimentalStdlibApi
fun generateTransactionId(): String {
val rng = SecureRandom()
val res = ByteArray(16)
rng.nextBytes(res)
return res.toHexString().uppercase()
}
/**
* Calculate the resulting size of base64-encoding data of the given length,
* including padding.
*/
fun calculateBase64EncodedLength(dataLength: Int): Int {
val blocks = (dataLength + 3 - 1) / 3
return blocks * 4
}
fun checkOrderIDOverflow(n: Int): Boolean {
if (n <= 0)
throw IllegalArgumentException()
val base = 10 + 26
return n >= base * base
}
private fun getDigitChar(x: Int): Char {
if (x < 10) {
return '0' + x
}
return 'A' + (x - 10)
}
fun computeOrderIDFromNumber(n: Int): String {
if (n <= 0)
throw IllegalArgumentException()
if (checkOrderIDOverflow(n))
throw IllegalArgumentException()
var ni = n
val base = 10 + 26
val x1 = ni % base
ni = ni / base
val x2 = ni % base
val c1 = getDigitChar(x1)
val c2 = getDigitChar(x2)
return String(charArrayOf('O', 'R', c2, c1))
}
}
|