📄 hammingdistancematchrule.java
字号:
/*================= * Copyright (C) 2001 Dennis Chao * * Lisys is a program that monitors TCP SYN packets to detect network * traffic anomalies. * * Licensed under the GNU General Public License (GPL), version 2 or * higher. Please see the COPYING and PATENT files included with the * Lisys distribution, which can be found at: * * http://www.cs.unm.edu/~judd/lisys/ * * Also, the current text of the GPL can be found at: * * http://www.gnu.org/copyleft/gpl.html * * Note that Lisys has NO WARRANTY! *=================*/package edu.unm.cs.lisys.detection.matchrule;import edu.unm.cs.lisys.debug.*;import edu.unm.cs.lisys.util.*;import edu.unm.cs.lisys.detection.bip.*;import java.util.*;import java.io.*;/**========== * HammingDistanceMatchRule.java * (based on ContiguousMatchesMatchRule.java) * * Here are the people who have worked on this code, starting with * the most recent: * @author Justin Balthrop <judd@cs.unm.edu> * @author Dennis Chao <dlchao@cs.unm.edu> * * Hamming distance rule (number of same bits). * Permutation masks don't change the result. *==========*/public class HammingDistanceMatchRule implements MatchRule, Serializable{ public static void main(String[] args) { KnuthRandom random = new KnuthRandom(100); for (int i = 0; i < 5; i++) { BinaryInputPattern bip1 = new TCPDump49BitBIP(); bip1.constructBinaryString(random); BinaryInputPattern bip2 = new TCPDump49BitBIP(); bip2.constructBinaryString(random); System.err.println(bip1.toString()); System.err.println(bip2.toString()); MatchRule mr = new HammingDistanceMatchRule(); int length = mr.match(bip1, bip2); System.err.println("length of match = " + length); } } /**========== * match: * @param first binary input pattern to match * @param second binary input pattern to match * @return the number of bits the patterns have in common *==========*/ public int match(BinaryInputPattern first, BinaryInputPattern second) { BitSet bits1 = (BitSet) first.getBinaryString().clone(); BitSet bits2 = (BitSet) second.getBinaryString().clone(); bits1.xor(bits2); // Now, search for the number of xored bits set to 1. int currentMatches = 0; for (int i = 0; i < first.getLength(); i++) { boolean bit = bits1.get(i); if (bit != true) { currentMatches++; } } return currentMatches; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -