Skip to content

Commit 5dee06b

Browse files
committed
Correct the function signatures so they follow the spec and implementation
See #41
1 parent 89ff89f commit 5dee06b

File tree

4 files changed

+43
-25
lines changed

4 files changed

+43
-25
lines changed

src/main/java/org/expath/exist/crypto/digest/HashFunction.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,19 @@ public class HashFunction extends BasicFunction {
5858
"The cryptographic hashing algorithm.");
5959

6060
public static final FunctionSignature FS_HASH[] = functionSignatures(FS_HASH_NAME,
61-
"resulting hash value, as string.", returnsOptMany(Type.BYTE),
62-
arities(arity(FS_HASH_PARAM_DATA, FS_HASH_PARAM_ALGORITHM),
63-
arity(FS_HASH_PARAM_DATA, FS_HASH_PARAM_ALGORITHM, param("encoding", Type.STRING,
64-
"The encoding of the output. The legal values are \"hex\" and \"base64\". The default value is \"base64\"."))));
61+
"resulting hash value, as string.", returnsOpt(Type.STRING),
62+
arities(
63+
arity(
64+
FS_HASH_PARAM_DATA,
65+
FS_HASH_PARAM_ALGORITHM
66+
),
67+
arity(
68+
FS_HASH_PARAM_DATA,
69+
FS_HASH_PARAM_ALGORITHM,
70+
optParam("encoding", Type.STRING, "The encoding of the output. The legal values are \"hex\" and \"base64\". The default value is \"base64\".")
71+
)
72+
)
73+
);
6574

6675
public HashFunction(final XQueryContext context, final FunctionSignature signature) {
6776
super(context, signature);

src/main/java/org/expath/exist/crypto/digest/HmacFunction.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
*/
2020
package org.expath.exist.crypto.digest;
2121

22-
import static org.exist.xquery.FunctionDSL.arities;
23-
import static org.exist.xquery.FunctionDSL.arity;
24-
import static org.exist.xquery.FunctionDSL.optManyParam;
25-
import static org.exist.xquery.FunctionDSL.param;
26-
import static org.exist.xquery.FunctionDSL.returnsOptMany;
22+
import static org.exist.xquery.FunctionDSL.*;
2723
import static org.expath.exist.crypto.ExistExpathCryptoModule.functionSignatures;
2824

2925
import java.io.IOException;
@@ -60,10 +56,21 @@ public class HmacFunction extends BasicFunction {
6056
"The cryptographic hashing algorithm.");
6157

6258
public final static FunctionSignature FS_HMAC[] = functionSignatures(FS_HMAC_NAME, "Hashes the input message.",
63-
returnsOptMany(Type.BYTE),
64-
arities(arity(FS_HMAC_PARAM_DATA, FS_HMAC_PARAM_KEY, FS_HMAC_PARAM_ALGORITHM),
65-
arity(FS_HMAC_PARAM_DATA, FS_HMAC_PARAM_KEY, FS_HMAC_PARAM_ALGORITHM, param("encoding", Type.STRING,
66-
"The encoding of the output. The legal values are \"hex\" and \"base64\". The result is generated accordingly as xs:base64Binary string or xs:hexBinary string."))));
59+
returnsOpt(Type.STRING),
60+
arities(
61+
arity(
62+
FS_HMAC_PARAM_DATA,
63+
FS_HMAC_PARAM_KEY,
64+
FS_HMAC_PARAM_ALGORITHM
65+
),
66+
arity(
67+
FS_HMAC_PARAM_DATA,
68+
FS_HMAC_PARAM_KEY,
69+
FS_HMAC_PARAM_ALGORITHM,
70+
param("encoding", Type.STRING, "The encoding of the output. The legal values are \"hex\" and \"base64\". The result is generated accordingly as xs:base64Binary string or xs:hexBinary string.")
71+
)
72+
)
73+
);
6774

6875
public HmacFunction(final XQueryContext context, final FunctionSignature signature) {
6976
super(context, signature);
@@ -87,24 +94,25 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
8794
final String algorithm = args[2].getStringValue();
8895
LOG.debug("algorithm = {}", algorithm);
8996

97+
final String encoding;
98+
final String resultString;
9099
if (argsLength == 3) {
100+
encoding = "base64";
91101
final byte[] resultBytes;
92102
if (data.isLeft()) {
93103
try (final InputStream is = data.left().get()) {
94-
resultBytes = Hmac.hmac(is, secretKey, algorithm);
104+
resultString = Hmac.hmac(is, secretKey, algorithm, encoding);
95105
}
96106
dataStreamClosed = true;
97107
} else {
98-
resultBytes = Hmac.hmac(data.right().get(), secretKey, algorithm);
108+
resultString = Hmac.hmac(data.right().get(), secretKey, algorithm, encoding);
99109
}
100110

101-
result = Conversion.byteArrayToIntegerSequence(resultBytes);
111+
result = new StringValue(resultString);
102112
} else if (argsLength == 4) {
103-
final String encoding = args[3].getStringValue().isEmpty() ? "base64" : args[3].getStringValue();
113+
encoding = args[3].getStringValue().isEmpty() ? "base64" : args[3].getStringValue();
104114
LOG.debug("encoding = {}", encoding);
105115

106-
final String resultString;
107-
108116
if (data.isLeft()) {
109117
try (final InputStream is = data.left().get()) {
110118
resultString = Hmac.hmac(is, secretKey, algorithm, encoding);

src/main/java/org/expath/exist/crypto/digitalSignature/GenerateSignatureFunction.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.security.PrivateKey;
3333
import java.util.Optional;
3434

35+
import javax.annotation.Nullable;
3536
import javax.xml.crypto.dsig.XMLSignatureException;
3637
import javax.xml.parsers.DocumentBuilder;
3738
import javax.xml.parsers.DocumentBuilderFactory;
@@ -211,7 +212,7 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
211212
Document signatureDocument = null;
212213

213214
// get the XPath expression and/or the certificate's details
214-
String xpathExprString = null;
215+
@Nullable String xpathExprString = null;
215216
String[] certificateDetails = new String[5];
216217
certificateDetails[0] = "";
217218
InputStream keyStoreInputStream = null;
@@ -220,7 +221,7 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
220221

221222
// signature with 7 arguments
222223
if (args.length == 7) {
223-
if (args[6].itemAt(0).getType() == 22) {
224+
if (args[6].getItemCount() > 0 && args[6].itemAt(0).getType() == Type.STRING) {
224225
xpathExprString = args[6].getStringValue();
225226
} else if (args[6].itemAt(0).getType() == 1) {
226227
final Node certificateDetailsNode = ((NodeValue) args[6].itemAt(0)).getNode();
@@ -233,7 +234,9 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
233234

234235
// signature with 8 arguments
235236
if (args.length == 8) {
236-
xpathExprString = args[6].getStringValue();
237+
if (args[6].getItemCount() > 0) {
238+
xpathExprString = args[6].getStringValue();
239+
}
237240
final Node certificateDetailsNode = ((NodeValue) args[7].itemAt(0)).getNode();
238241
// get the certificate details
239242
certificateDetails = getDigitalCertificateDetails(certificateDetails, certificateDetailsNode);

src/main/java/org/expath/exist/crypto/encrypt/EncryptionFunctions.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ private Sequence encrypt(byte[] data, CryptType encryptType, String secretKey, S
139139
LOG.debug("encrypt result = {}", result);
140140

141141
return new StringValue(result);
142-
} catch (
143-
144-
CryptoException e) {
142+
} catch (final CryptoException e) {
145143
throw new EXpathCryptoException(this, e.getCryptoError());
146144
} catch (IOException e) {
147145
throw new EXpathCryptoException(this, e);

0 commit comments

Comments
 (0)