Skip to content

Commit

Permalink
Merge pull request #136 from usdAG/feat/luhn-checksum
Browse files Browse the repository at this point in the history
Add Luhn checksum operation
  • Loading branch information
felixb1515 authored May 14, 2024
2 parents 5dc767e + 88c0e19 commit 82f757f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 27 deletions.
55 changes: 28 additions & 27 deletions src/main/java/de/usd/cstchef/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
import de.usd.cstchef.operations.hashing.SM3;
import de.usd.cstchef.operations.hashing.Skein;
import de.usd.cstchef.operations.hashing.Tiger;
import de.usd.cstchef.operations.hashing.Luhn;
import de.usd.cstchef.operations.hashing.Whirlpool;
import de.usd.cstchef.operations.misc.GetRequestBuilder;
import de.usd.cstchef.operations.misc.ReadFile;
Expand Down Expand Up @@ -286,33 +287,33 @@ public static Class<? extends Operation>[] getOperationsBurp() {
@SuppressWarnings("unchecked")
public static Class<? extends Operation>[] getOperationsDev() {
return new Class[] {
Addition.class, AddKey.class, AesDecryption.class, AesEncryption.class, And.class,
Blake.class, Counter.class, DateTime.class, Deflate.class, DesDecryption.class, DesEncryption.class,
Divide.class, DivideList.class, DSTU7564.class, FromBase64.class, FromHex.class,
GetRequestBuilder.class,
GetVariable.class, Gost.class, GUnzip.class, Gzip.class, Hmac.class,
HttpBodyExtractor.class, HttpCookieExtractor.class, HttpGetExtractor.class,
HttpGetSetter.class, HttpHeaderExtractor.class, HttpHeaderSetter.class,
HttpJsonExtractor.class, HttpJsonSetter.class, HttpMethodExtractor.class, HttpMultipartExtractor.class,
HttpMultipartSetter.class,
HttpPostExtractor.class, HttpPostSetter.class, PlainRequest.class, HttpSetBody.class,
HttpSetCookie.class, HttpSetUri.class, HttpUriExtractor.class, HttpXmlExtractor.class,
HttpXmlSetter.class, HtmlEncode.class, HtmlDecode.class, Inflate.class,
JsonExtractor.class, JsonSetter.class, JWTDecode.class, JWTSign.class, Length.class,
LineExtractor.class,
LineSetter.class, MD2.class, MD4.class, MD5.class, Mean.class, Median.class,
Multiply.class, MultiplyList.class, NoOperation.class, NumberCompare.class, Prefix.class,
RandomNumber.class, RandomUUID.class, ReadFile.class, RegexExtractor.class, Reverse.class,
Replace.class,
RIPEMD.class, RsaDecryption.class, RsaEncryption.class, RsaSignature.class, SM2Signature.class, SM3.class, SM4Encryption.class, SM4Decryption.class, RegexMatch.class,
SetIfEmpty.class, SHA1.class, SHA2.class, SHA3.class, Skein.class, SplitAndSelect.class,
StaticString.class, StoreVariable.class, Sub.class, Substring.class, Uppercase.class, Lowercase.class,
Subtraction.class,
Suffix.class, Sum.class, StringContains.class, StringMatch.class, Tiger.class,
TimestampOffset.class, TimestampToDateTime.class, ToBase64.class, ToHex.class, UnixTimestamp.class,
UrlDecode.class, UrlEncode.class,
Whirlpool.class, WriteFile.class, XmlFullSignature.class, XmlMultiSignature.class,
Xor.class, SoapMultiSignature.class
Addition.class, AddKey.class, AesDecryption.class, AesEncryption.class, And.class,
Blake.class, Counter.class, DateTime.class, Deflate.class, DesDecryption.class, DesEncryption.class,
Divide.class, DivideList.class, DSTU7564.class, FromBase64.class, FromHex.class,
GetRequestBuilder.class,
GetVariable.class, Gost.class, GUnzip.class, Gzip.class, Hmac.class,
HttpBodyExtractor.class, HttpCookieExtractor.class, HttpGetExtractor.class,
HttpGetSetter.class, HttpHeaderExtractor.class, HttpHeaderSetter.class,
HttpJsonExtractor.class, HttpJsonSetter.class, HttpMethodExtractor.class, HttpMultipartExtractor.class,
HttpMultipartSetter.class,
HttpPostExtractor.class, HttpPostSetter.class, PlainRequest.class, HttpSetBody.class,
HttpSetCookie.class, HttpSetUri.class, HttpUriExtractor.class, HttpXmlExtractor.class,
HttpXmlSetter.class, HtmlEncode.class, HtmlDecode.class, Inflate.class,
JsonExtractor.class, JsonSetter.class, JWTDecode.class, JWTSign.class, Length.class,
LineExtractor.class,
LineSetter.class, MD2.class, MD4.class, MD5.class, Mean.class, Median.class,
Multiply.class, MultiplyList.class, NoOperation.class, NumberCompare.class, Prefix.class,
RandomNumber.class, RandomUUID.class, ReadFile.class, RegexExtractor.class, Reverse.class,
Replace.class,
RIPEMD.class, RsaDecryption.class, RsaEncryption.class, RsaSignature.class, SM2Signature.class, SM3.class, SM4Encryption.class, SM4Decryption.class, RegexMatch.class,
SetIfEmpty.class, SHA1.class, SHA2.class, SHA3.class, Skein.class, SplitAndSelect.class,
StaticString.class, StoreVariable.class, Sub.class, Substring.class, Uppercase.class, Lowercase.class,
Subtraction.class,
Suffix.class, Sum.class, StringContains.class, StringMatch.class, Tiger.class,
TimestampOffset.class, TimestampToDateTime.class, ToBase64.class, ToHex.class, UnixTimestamp.class,
UrlDecode.class, UrlEncode.class,
Whirlpool.class, WriteFile.class, XmlFullSignature.class, XmlMultiSignature.class,
Xor.class, SoapMultiSignature.class, Luhn.class
};
}

Expand Down
49 changes: 49 additions & 0 deletions src/main/java/de/usd/cstchef/operations/hashing/Luhn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.usd.cstchef.operations.hashing;

import burp.api.montoya.core.ByteArray;
import de.usd.cstchef.Utils.MessageType;
import de.usd.cstchef.operations.Operation;
import de.usd.cstchef.operations.OperationCategory;
import de.usd.cstchef.operations.Operation.OperationInfos;


@OperationInfos(name = "Luhn", category = OperationCategory.HASHING, description = "Calculate Luhn of a number")
public class Luhn extends Operation {

@Override
protected ByteArray perform(ByteArray input, MessageType messageType) throws Exception {

for (int i = 0; i < input.length(); i++){
if ((input.getByte(i) < '0') || (input.getByte(i) > '9')) {
throw new IllegalArgumentException("Luhn can only be applied to numerical values.");
}
}

int check_digit = calculateLuhnCheckDigit(input);
return factory.createByteArray((byte)check_digit + '0');
}

private int calculateLuhnCheckDigit(ByteArray input) {
int sum = 0;
boolean doubleDigit = true;

for (int i = input.length() - 1; i >= 0; i--) {



int digit = Integer.valueOf(Character.toString ((char)input.getByte(i)));

if (doubleDigit) {
digit *= 2;
if (digit > 9) {
digit -= 9;
}
}
sum += digit;
doubleDigit = !doubleDigit;
}

int mod = sum % 10;
return mod == 0 ? 0 : 10 - mod;
}
}

0 comments on commit 82f757f

Please sign in to comment.