package cn.hobom.mobile.datacollector.util;
import java.io.Serializable;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Random;
import java.util.function.BiFunction;
import java.util.zip.Adler32;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
/**
*
*/
public class HashProvider {
private static final int seed32 = 89478583;
/**
* @param a the byte array to be hashed
* @return the 32 bit integer hash value
*/
static int hashBytes(byte a[]) {
// 32 bit FNV constants. Using longs as Java does not support unsigned
// datatypes.
final long FNV_PRIME = 16777619;
final long FNV_OFFSET_BASIS = 2166136261l;
if (a == null)
return 0;
long result = FNV_OFFSET_BASIS;
for (byte element : a) {
result = (result * FNV_PRIME) & 0xFFFFFFFF;
result ^= element;
}
// return Arrays.hashCode(a);
return (int) result;
}
/**
* @param value the value to be hashed
* @param m integer output range [1,size]
* @param k number of hashes to be computed
* @return array with <i>hashes</i> integer hash positions in the range <i>[0,size)</i>
*/
public static int[] hashCarterWegman(byte[] value, int m, int k) {
int[] positions = new int[k];
BigInteger prime32 = BigInteger.valueOf(4294967279l);
BigInteger prime64 = BigInteger.valueOf(53200200938189l);
BigInteger prime128 = new BigInteger("21213943449988109084994671");
Random r = new Random(seed32);
//BigInteger.valueOf(hashBytes(value)
BigInteger v = new BigInteger(value.length > 0 ? value : new byte[1]);
for (int i = 0; i < k; i++) {
BigInteger a = BigInteger.valueOf(r.nextLong());
BigInteger b = BigInteger.valueOf(r.nextLong());
positions[i] = a.multiply(v).add(b).mod(prime64)
.mod(BigInteger.valueOf(m)).intValue();
}
return positions;
}
/**
* @param value the value to be hashed
* @param m integer output range [1,size]
* @param k number of hashes to be computed
* @return array with <i>hashes</i> integer hash positions in the range <i>[0,size)</i>
*/
public static int[] hashRNG(byte[] value, int m, int k) {
int[] positions = new int[k];
Random r = new Random(hashBytes(value));
for (int i = 0; i < k; i++) {
positions[i] = r.nextInt(m);
}
return positions;
}
/**
* @param value the value to be hashed
* @param m integer output range [1,size]
* @param k number of hashes to be computed
* @return array with <i>hashes</i> integer hash positions in the range <i>[0,size)</i>
*/
public static int[] hashCRC(byte[] value, int m, int k) {
return hashChecksum(value, new CRC32(), m, k);
}
/**
* @param value the value to be hashed
* @param m integer output range [1,size]
* @param k number of hashes to be computed
* @return array with <i>hashes</i> integer hash positions in the range <i>[0,size)</i>
*/
public static int[] hashAdler(byte[] value, int m, int k) {
return hashChecksum(value, new Adler32(), m, k);
}
public static int[] hashChecksum(byte[] value, Checksum cs, int m, int k) {
int[] positions = new int[k];
int hashes = 0;
int salt = 0;
while (hashes < k) {
cs.reset();
cs.update(value, 0, value.length);
// Modify the data to be checksummed by adding the number of already
// calculated hashes, the loop counter and
// a static seed
cs.update(hashes + salt++ + seed32);
int hash = rejectionSample((int) cs.getValue(), m);
if (hash != -1) {
positions[hashes++] = hash;
}
}
return positions;
}
/**
* @param value the value to be hashed
* @param m integer output range [1,size]
* @param k number of hashes to be computed
* @return array with <i>hashes</i> integer hash positions in the range <i>[0,size)</i>
*/
public static int[] hashSimpleLCG(byte[] value, int m, int k) {
// Java constants
final long multiplier = 0x5DEECE66DL;
final long addend = 0xBL;
final long mask = (1L << 48) - 1;
// Generate int from byte Array using the FNV hash
int reduced = Math.abs(hashBytes(value));
// Make number positive
// Handle the special case: smallest negative number is itself as the
// absolute value
if (reduced == Integer.MIN_VALUE)
reduced = 42;
// Calculate hashes numbers iteratively
int[] positions = new int[k];
long seed = reduced;
for (int i = 0; i < k; i++) {
// LCG formula: x_i+1 = (multiplier * x_i + addend) mod mask
seed = (seed * multiplier + addend) & mask;
positions[i] = (int) (seed >>> (48 - 30)) % m;
}
return positions;
}
public static int[] hashMurmur3(byte[] value, int m, int k) {
return rejectionSample(HashProvider::murmur3, value, m, k);
}
public static int[] hashCassandra(byte[] value, int m, int k) {
int[] result = new int[k];
int hash1 = murmur3(0, value);
int hash2 = murmur3((int) hash1, value);
for (int i = 0; i < k; i++) {
result[i] = Math.abs((hash1 + i * hash2) % m);
}
return result;
}
public static int murmur3(int seed, byte[] bytes) {
int h1 = seed; //Standard in Guava
int c1 = 0xcc9e2d51;
int c2 = 0x1b873593;
int len = bytes.length;
int i = 0;
while (len >= 4) {
//process()
int k1 = bytes[i + 0] & 0xFF;
k1 |= (bytes[i + 1] & 0xFF) << 8;
k1 |= (bytes[i + 2] & 0xFF) << 16;
k1 |= (bytes[i + 3] & 0xFF) << 24;
k1 *= c1;
k1 = Integer.rotateLeft(k1, 15);
k1 *= c2;
h1 ^= k1;
h1 = Integer.rotateLeft(h1, 13);
h1 = h1 * 5 + 0xe6546b64;
len -= 4;
i += 4;
}
if (len > 0) {
//processingRemaining()
int k1 = 0;
switch (len) {
case 3:
k1 ^= (bytes[i + 2] & 0xFF) << 16;
// fall through
case 2:
k1 ^= (bytes[i + 1] & 0xFF) << 8;
// fall through
case 1:
k1 ^= (bytes[i] & 0xFF);
// fall through
default:
k1 *= c1;
k1 = Integer.rotateLeft(k1, 15);
k1 *= c2;
h1 ^= k1;
}
i += len;
}
//makeHash()
h1 ^= i;
h1 ^= h1 >>> 16;
h1 *= 0x85ebca6b;
h1 ^= h1 >>> 13;
h1 *= 0xc2b2ae35;
h1 ^= h1 >>> 16;
return h1;
}
// Code taken from:
// http://dmy999.com/article/50/murmurhash-2-java-port by Derekt
// Young (Public Domain)
// as the Hadoop implementation by Andrzej Bialecki is buggy
public static int[] hashMurmur2(byte[] value, int em, int ka) {
int[] positions = new int[ka];
int hashes = 0;
int lastHash = 0;
byte[] data = value.clone();
while (hashes < ka) {
for (int i = 0; i < value.lengt
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Android 项目是使用 Android 操作系统和相关开发工具开发的一款移动应用程序。Android 平台提供了丰富的功能和接口,开发人员可以使用 Java 或 Kotlin 等编程语言编写 Android 应用程序。Android 项目也可以是针对特定设备或特定需求进行自定义开发的软件解决方案。 以下是 Android 项目的一些主要特点和资料介绍: 1. 开放源代码:Android 是基于 Linux 内核的开源操作系统,开发人员可以自由获取、使用和修改源代码。 2. 多样化的硬件设备支持:Android 支持多种硬件设备和屏幕尺寸,可以运行于手机、平板电脑、电视、手表等多种设备上。 3. 灵活的用户界面:Android 提供了丰富的用户界面控件和布局方式,可以实现漂亮、个性化的用户界面。 4. 响应式设计:Android 应用程序可以根据设备类型、屏幕尺寸等因素调整布局和显示方式,以适应不同的设备和用户需求。 5. 多媒体支持:Android 支持常见的音频、视频、图像等多媒体格式,可以实现各种多媒体应用。 6. 数据存储:Android 提供了多种数据存储方式,包括 SQLite 数据库、文件存储、SharedPreferences 等。 7. 网络通信:Android 支持多种网络通信方式,包括 HTTP、TCP、UDP 等。 8. 社交媒体集成:Android 提供了集成社交媒体的功能,可以实现与 Facebook、Twitter、Google+ 等社交媒体的交互。 # 注意 1. 本资源仅用于开源学习和技术交流。 2. 部分字体以及插图等来自网络,若是侵权请联系删除。 3. 不可商用,一切后果由使用者承担。
资源推荐
资源详情
资源评论
收起资源包目录
恶意软件检测系统Android端代码.zip (113个子文件)
gradlew.bat 2KB
.gitignore 127B
.gitignore 8B
build.gradle 1KB
build.gradle 777B
settings.gradle 16B
gradlew 5KB
json-lib-2.4.jar 155KB
gradle-wrapper.jar 52KB
HashProvider.java 19KB
TraceActivity.java 13KB
DataProcess.java 8KB
GetLocalTrace.java 8KB
DetectorActivity.java 7KB
ShellUtils.java 7KB
CommonUtil.java 6KB
LocalTraceActivity.java 6KB
DetectJob.java 5KB
MultipartEntity.java 5KB
FileBrowser.java 4KB
MainActivity.java 4KB
HeaderUtil.java 4KB
ApkTool.java 4KB
PreferencesUtil.java 3KB
Connector.java 3KB
AppAdapter.java 3KB
Job.java 2KB
SettingActivity.java 2KB
JobManager.java 2KB
ChineseToPinyin.java 2KB
CountedSystemcall.java 1KB
JSONUtil.java 1KB
AppInfo.java 1KB
UploadJob.java 1KB
TraceServerJob.java 1KB
Msg.java 1KB
HashSystemCall.java 949B
CollectorApplication.java 901B
StraceStart.java 891B
ExampleInstrumentedTest.java 788B
ServiceConfiguration.java 663B
ExampleUnitTest.java 423B
JobListener.java 173B
home_speed_bg.png 289KB
home_bg.png 286KB
home_n_speed.png 44KB
checkbox_on.png 23KB
checkbox_off.png 19KB
ic_launcher_round.png 14KB
ic_launcher.png 10KB
ic_launcher_round.png 10KB
switchon.png 9KB
ic_launcher.png 8KB
ic_launcher_round.png 6KB
switchoff.png 5KB
my_switch_on.png 5KB
ic_launcher.png 5KB
my_switch_off.png 5KB
back.png 5KB
ic_launcher_round.png 4KB
list_speed.png 4KB
ic_launcher.png 3KB
ic_launcher_round.png 2KB
ic_launcher.png 2KB
back_press.png 1KB
bigred.png 1KB
back_btn_touch.png 755B
back_btn.png 643B
btn_pressed.9.png 237B
btn_normal.9.png 227B
checkbox_background.9.png 178B
proguard-rules.pro 964B
gradle.properties 747B
gradle-wrapper.properties 232B
gradle_models.ser 90KB
build_file_checksums.ser 539B
colors.xml 5KB
styles.xml 4KB
Project.xml 3KB
misc.xml 2KB
app_item.xml 2KB
local_trace.xml 2KB
AndroidManifest.xml 2KB
trace.xml 2KB
header.xml 2KB
setting.xml 1KB
bt_nobgd.xml 1KB
bn_bg_red_white.xml 1KB
activity_main.xml 1KB
detect.xml 946B
bn_bg_red.xml 868B
bn_bg_red_no_border.xml 856B
gradle.xml 811B
bn_bg_white_no_boder.xml 733B
compiler.xml 727B
bn_bg_red_share.xml 650B
bn_bg_yellow_look.xml 650B
runConfigurations.xml 564B
roundbtn.xml 555B
checkbox_btn.xml 512B
共 113 条
- 1
- 2
资源评论
阿齐Archie
- 粉丝: 1w+
- 资源: 2301
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功