From b992cf9fba7e552d1f351e0f238057e50a9fa6b1 Mon Sep 17 00:00:00 2001 From: adash Date: Fri, 30 Oct 2009 20:05:49 +0000 Subject: [PATCH] initial signature calculator --- .../SpamFilter/SignatureComputer.java | 158 ++++++++++++++++++ .../Distributed/SpamFilter/SpamFilter.java | 11 +- 2 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 Robust/src/Benchmarks/Distributed/SpamFilter/SignatureComputer.java diff --git a/Robust/src/Benchmarks/Distributed/SpamFilter/SignatureComputer.java b/Robust/src/Benchmarks/Distributed/SpamFilter/SignatureComputer.java new file mode 100644 index 00000000..07611172 --- /dev/null +++ b/Robust/src/Benchmarks/Distributed/SpamFilter/SignatureComputer.java @@ -0,0 +1,158 @@ +public class SignatureComputer { + public EphemeralSignature sig4; //signature engines + public WhiplashSignature sig8; //signature engines + + int[] enginesToUseForCheck; + + public SignatureComputer() { + sig4 = new EphemeralSignature(); //default values + sig8 = new WhiplashSignature(); + createEnginesToCheck(); + } + + /** + * constructor to be used when some parsing has already taken place with the + * server-provides value randomNumberSeed. + * + * @param randomNumberSeed + * a non-negative number used for seeding the random number generator + * before starting to hash values. + * @param separator + * how the mail-text should be splitted into lines. (== what chars + * separate 2 lines) + */ + public SignatureComputer(int randomNumberSeed, String separator) { + sig4 = new EphemeralSignature(randomNumberSeed,separator); + sig8 = new WhiplashSignature(); + createEnginesToCheck(); + } + + /** + * the constructor to be used most of the time. you can hand over the + * seed-string exactly as it is provided by the razor-server. + * + * @param seedAndSeparator + * a string containing the seed value for the RNG and a separator list + * (separated by ' - '). default value is + * "7542-10" which means server-seed 7542 and only one + * separator 10 (which is ascii '\n'). + */ + public SignatureComputer(String seedAndSeparator) { + sig4 = new EphemeralSignature(seedAndSeparator); + sig8 = new WhiplashSignature(); + createEnginesToCheck(); + } + + /** + * + */ + public void createEnginesToCheck() { + enginesToUseForCheck = new int[2]; + enginesToUseForCheck[0] = 4; //Ephemeral engine + enginesToUseForCheck[1] = 8;//Whiplash engine + } + + public boolean isSigSupported(int sig) { + boolean found = false; + for (int i = 0; i < enginesToUseForCheck.length && !found; i++) { + if (enginesToUseForCheck[i] == sig) { + found = true; + } + } + return found; + } + + public boolean isSigSupported(String sig) { + return (sig != null && isSigSupported(Integer.parseInt(sig))); + } + + public String getDefaultEngine() { + return "4"; + } + + public Vector computeSigs(StringBuffer[] Mails) { + if (Mails == null) return null; + + Vector printableSigs = new Vector(); + for (int mailIndex = 0; mailIndex < Mails.length; mailIndex++) { + StringBuffer mail = Mails[mailIndex]; + + if (mail == null) continue; + + /* + * Compute Sig for bodyparts that are cleaned. + */ + for (int engineIndex = 0; engineIndex < enginesToUseForCheck.length; engineIndex++) { + int engineNo = enginesToUseForCheck[engineIndex]; + String[] sig = null; + + switch (engineNo) { + case 4: + sig = computeSignature(engineNo,curPart.getCleaned()); + break; + case 8: + sig = computeSignature(engineNo,curPart.getBody()); + break; + default: + /* + * for nilsimsa and sha1 wich are no longer supported by + * the server and might be removed someday + */ + sig = computeSignature(engineNo,curPart.getCleaned()); + break; + }//switch engineNo + + if (sig != null && sig.length > 0) { + for (int curSigIndex = 0; curSigIndex < sig.length; curSigIndex++) { + String hash = engineNo + ":" + sig[curSigIndex]; + curPart.addHash(hash); + printableSigs.add(hash); + } + + } else { + /* we didn't produce a signature for the mail. */ + } + }//engine + }//mails + return printableSigs; + }//computeSigs + + /** + * @param engineNo + * @param cleaned + * @return + */ + private String[] computeSignature(int engineNo, String mail) { + switch (engineNo) { + case 4: + return new String[] { this.sig4.computeSignature(mail) }; + case 8: + String cleanedButKeepHTML = Preprocessor.preprocess(mail,Preprocessor.ConfigParams.NO_DEHTML); + return this.sig8.computeSignature(cleanedButKeepHTML); + default: + return null; + } + } + + public static String[] getCommonSupportedEngines(int serverSupportedEngines) { + Vector commonSupported = new Vector(); + int engineMask = 1; + int engineIndex = 1; + while (engineIndex < 32) { + boolean serverSupported = (serverSupportedEngines & engineMask) > 0; + boolean clientSupported = isSigSupported(engineIndex); + if (serverSupported && clientSupported) { + commonSupported.add(String.valueOf(engineIndex)); + } + //switch to next + engineMask <<= 1; //shift one to left + engineIndex++; + } + if (commonSupported.size() == 0) { + return null; + } + String[] result = new String[commonSupported.size()]; + commonSupported.toArray(result); + return result; + } +} diff --git a/Robust/src/Benchmarks/Distributed/SpamFilter/SpamFilter.java b/Robust/src/Benchmarks/Distributed/SpamFilter/SpamFilter.java index f65ba8ba..7a9b62b4 100644 --- a/Robust/src/Benchmarks/Distributed/SpamFilter/SpamFilter.java +++ b/Robust/src/Benchmarks/Distributed/SpamFilter/SpamFilter.java @@ -110,13 +110,16 @@ public class SpamFilter extends Thread { **/ public FilterResult[] checkMail(Mail mail) { //Preprocess emails - //StringBuffer[] mailStrings = createMailStrings(); + //StringBuffer[] partsOfMailStrings = createMailStrings(); + //RazorMail[] razorMails = //Compute signatures - //CommunicationEngine checkEngine = getCheckEngine(); - //SignatureComputer sigComp = new SignatureComputer(); + SignatureComputer sigComp = new SignatureComputer(); + Vector signatures = sigComp.computeSigs(partsOfMailStrings);//vector of strings + //check with global data structure + check(signatures); - //return results + //---- create and return results -------- FilterResult[] filterResults = new FilterResult[mailStrings.length]; return filterResults; -- 2.34.1