Skip to content

Commit f7f0025

Browse files
committed
#65 make padding pass a Pointer so we can return the length
1 parent 7eb977e commit f7f0025

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

src/main/java/com/goterl/lazycode/lazysodium/LazySodium.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.goterl.lazycode.lazysodium.utils.*;
1414
import com.sun.jna.NativeLong;
1515
import com.sun.jna.Pointer;
16+
import com.sun.jna.ptr.IntByReference;
1617
import com.sun.jna.ptr.PointerByReference;
1718

1819
import java.nio.charset.Charset;
@@ -175,13 +176,13 @@ public byte[] randomBytesDeterministic(int size, byte[] seed) {
175176
//// -------------------------------------------|
176177

177178
@Override
178-
public boolean sodiumPad(int paddedBuffLen, char[] buf, int unpaddedBufLen, int blockSize, int maxBufLen) {
179-
return successful(getSodium().sodium_pad(paddedBuffLen, buf, unpaddedBufLen, blockSize, maxBufLen));
179+
public boolean sodiumPad(IntByReference paddedBuffLen, char[] buf, int unpaddedBufLen, int blockSize, int maxBufLen) {
180+
return successful(getSodium().sodium_pad(paddedBuffLen.getPointer(), buf, unpaddedBufLen, blockSize, maxBufLen));
180181
}
181182

182183
@Override
183-
public boolean sodiumUnpad(int unPaddedBuffLen, char[] buf, int paddedBufLen, int blockSize) {
184-
return successful(getSodium().sodium_unpad(unPaddedBuffLen, buf, paddedBufLen, blockSize));
184+
public boolean sodiumUnpad(IntByReference unPaddedBuffLen, char[] buf, int paddedBufLen, int blockSize) {
185+
return successful(getSodium().sodium_unpad(unPaddedBuffLen.getPointer(), buf, paddedBufLen, blockSize));
185186
}
186187

187188

src/main/java/com/goterl/lazycode/lazysodium/Sodium.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public native int sodium_base642bin(byte[] bin,
9292
//// PADDING
9393
//// -------------------------------------------|
9494

95-
public native int sodium_pad(int paddedBuffLen, char[] buf, int unpaddedBufLen, int blockSize, int maxBufLen);
95+
public native int sodium_pad(Pointer paddedBuffLen, char[] buf, int unpaddedBufLen, int blockSize, int maxBufLen);
9696

97-
public native int sodium_unpad(int paddedBuffLen, char[] buf, int unpaddedBufLen, int blockSize);
97+
public native int sodium_unpad(Pointer paddedBuffLen, char[] buf, int unpaddedBufLen, int blockSize);
9898

9999

100100

src/main/java/com/goterl/lazycode/lazysodium/interfaces/Padding.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
package com.goterl.lazycode.lazysodium.interfaces;
1010

1111

12+
import com.sun.jna.ptr.IntByReference;
13+
1214
public interface Padding {
1315

1416
interface Native {
@@ -25,19 +27,19 @@ interface Native {
2527
* to be.
2628
* @return False if the padded buffer length would exceed {@code maxBufLen}.
2729
*/
28-
boolean sodiumPad(int paddedBuffLen, char[] buf, int unpaddedBufLen, int blockSize, int maxBufLen);
30+
boolean sodiumPad(IntByReference paddedBuffLen, char[] buf, int unpaddedBufLen, int blockSize, int maxBufLen);
2931

3032
/**
3133
* Computes the original, unpadded length of a message previously padded using
32-
* {@link #sodiumPad(int, char[], int, int, int)}. The original length is put into
34+
* {@link #sodiumPad(IntByReference, char[], int, int, int)}. The original length is put into
3335
* {@code unpaddedBufLen}.
3436
* @param unpaddedBufLen This will be populated with the unpadded buffer length.
3537
* @param buf The buffer.
3638
* @param paddedBufLen The padded buffer size.
3739
* @param blockSize The block size.
3840
* @return True if the buffer was unpadded.
3941
*/
40-
boolean sodiumUnpad(int unpaddedBufLen, char[] buf, int paddedBufLen, int blockSize);
42+
boolean sodiumUnpad(IntByReference unpaddedBufLen, char[] buf, int paddedBufLen, int blockSize);
4143
}
4244

4345
interface Lazy {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) Terl Tech Ltd • 14/06/19 17:54 • goterl.com
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v2.0. If a copy of the MPL was not distributed with this
6+
* file, you can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
package com.goterl.lazycode.lazysodium;
10+
import com.sun.jna.ptr.IntByReference;
11+
import junit.framework.TestCase;
12+
import org.junit.Test;
13+
14+
public class PaddingTest extends BaseTest {
15+
16+
17+
@Test
18+
public void pad() {
19+
IntByReference ref = new IntByReference(0);
20+
char[] b = new char[4];
21+
22+
lazySodium.sodiumPad(ref, b, 4, 4, 10);
23+
TestCase.assertEquals(8, ref.getValue());
24+
}
25+
26+
}

0 commit comments

Comments
 (0)