在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:yomorun/hashids-java开源软件地址:https://github.com/yomorun/hashids-java开源编程语言:Java 100.0%开源软件介绍:Hashids.javaA small Java class to generate YouTube-like hashes from one or many numbers. Ported from javascript hashids.js by Ivan Akimov What is it?hashids (Hash ID's) creates short, unique, decodable hashes from unsigned (long) integers. It was designed for websites to use in URL shortening, tracking stuff, or making pages private (or at least unguessable). This algorithm tries to satisfy the following requirements:
Instead of showing items as All (long) integers need to be greater than or equal to zero. UsageAdd the dependencyhashids is available in Maven Central. If you are using Maven, add the following dependency to your pom.xml's dependencies:
Alternatively, if you use gradle or are on android, add the following to your app's
Import the packageimport org.hashids; Encoding one numberYou can pass a unique salt value so your hashes differ from everyone else's. I use "this is my salt" as an example. Hashids hashids = new Hashids("this is my salt");
String hash = hashids.encode(12345L);
DecodingNotice during decoding, same salt value is used: Hashids hashids = new Hashids("this is my salt");
long[] numbers = hashids.decode("NkK9");
Decoding with different saltDecoding will not work if salt is changed: Hashids hashids = new Hashids("this is my pepper");
long[] numbers = hashids.decode("NkK9");
Encoding several numbersHashids hashids = new Hashids("this is my salt");
String hash = hashids.encode(683L, 94108L, 123L, 5L);
Decoding is done the same wayHashids hashids = new Hashids("this is my salt");
long[] numbers = hashids.decode("aBMswoO2UB3Sj");
Encoding and specifying minimum hash lengthHere we encode integer 1, and set the minimum hash length to 8 (by default it's 0 -- meaning hashes will be the shortest possible length). Hashids hashids = new Hashids("this is my salt", 8);
String hash = hashids.encode(1L);
DecodingHashids hashids = new Hashids("this is my salt", 8);
long[] numbers = hashids.decode("gB0NV05e");
Specifying custom hash alphabetHere we set the alphabet to consist of only six letters: "0123456789abcdef" Hashids hashids = new Hashids("this is my salt", 0, "0123456789abcdef");
String hash = hashids.encode(1234567L);
Encoding and decoding "MongoDB" idsIn addition to encoding and decoding long values Hashids provides functionality for encoding and decoding ids in a hex notation such as object ids generated by MongoDB. Hashids hashids = new Hashids("This is my salt");
String hash = hashids.encodeHex("507f1f77bcf86cd799439011"); // goMYDnAezwurPKWKKxL2
String objectId = hashids.decodeHex(hash); // 507f1f77bcf86cd799439011 Note that the algorithm used for encoding and decoding hex values is not
compatible with the algorthm for encoding and decoding long values. That
means that you cannot use RandomnessThe primary purpose of hashids is to obfuscate ids. It's not meant or tested to be used for security purposes or compression. Having said that, this algorithm does try to make these hashes unguessable and unpredictable: Repeating numbersHashids hashids = new Hashids("this is my salt");
String hash = hashids.encode(5L, 5L, 5L, 5L); You don't see any repeating patterns that might show there's 4 identical numbers in the hash:
Same with incremented numbers: Hashids hashids = new Hashids("this is my salt");
String hash = hashids.encode(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L);
Incrementing number hashes:Hashids hashids = new Hashids("this is my salt");
String hash1 = hashids.encode(1L); /* NV */
String hash2 = hashids.encode(2L); /* 6m */
String hash3 = hashids.encode(3L); /* yD */
String hash4 = hashids.encode(4L); /* 2l */
String hash5 = hashids.encode(5L); /* rD */ Bad hashesI wrote this class with the intent of placing these hashes in visible places - like the URL. If I create a unique hash for each user, it would be unfortunate if the hash ended up accidentally being a bad word. Imagine auto-creating a URL with hash for your user that looks like this - Therefore, this algorithm tries to avoid generating most common English curse words with the default alphabet. This is done by never placing the following letters next to each other:
LimitationsThe original and reference implementation is the JS (Hashids Website) version. JS ContributorsContactFollow me C.C.(@fanweixiao), @IvanAkimov, @0x3333. LicenseMIT License. See the |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论