Giter VIP home page Giter VIP logo

cuid-java's Introduction

CUID for Java

GitHub license Repository release Maven Repository size

Javadoc

Java implementation of CUID. Read more at CUID official website.


Build

To compile CUID for Java, you must ensure that Java 11 (or above) and Maven are correctly installed.

#> mvn package
#> mvn install

To speed up process, you can ignore unit tests by using: -DskipTests=true -Dmaven.test.skip=true.

How to use

<dependency>
  <groupId>io.github.thibaultmeyer</groupId>
  <artifactId>cuid</artifactId>
  <version>x.y.z</version>
</dependency>
final CUID cuid = CUID.randomCUID1();
System.out.println("CUID: " + cuid);
final CUID cuid = CUID.randomCUID2();
System.out.println("CUID (Version 2): " + cuid);
final int customLength = 8;  // Length must be, at least, 1
final CUID cuid = CUID.randomCUID2(customLength);
System.out.println("CUID (Version 2): " + cuid);
final CUID cuid = CUID.fromString("cl9gts1kw00393647w1z4v2tc");
System.out.println("CUID: " + cuid);
final boolean isValid = CUID.isValid("cl9gts1kw00393647w1z4v2tc");
System.out.println("Is 'cl9gts1kw00393647w1z4v2tc' a valid CUID ? " + isValid);

License

This project is released under terms of the MIT license.

cuid-java's People

Contributors

thibaultmeyer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cuid-java's Issues

Please provide validation `isCuid` feature provided by Cuid v2.2.0 (Upstream, Official JS)

Is your feature request related to a problem? Please describe.
https://github.com/paralleldrive/cuid2#validation

import { createId, isCuid } from '@paralleldrive/cuid2';

console.log(
  isCuid(createId()), // true
  isCuid('not a cuid'), // false
);

Describe the solution you'd like
Java version of https://github.com/paralleldrive/cuid2/pull/38/files

Describe alternatives you've considered
N/A

Additional context
paralleldrive/cuid2#37
paralleldrive/cuid2#38
https://github.com/paralleldrive/cuid2/releases/tag/v2.2.0

Deploy to maven central

I found this repo mentioned in https://usecuid.org/. It would be great if this library could be automatically deployed to Maven Central repo, so that people could reference it as a Maven dependency.

Really the Cuid2 algorithm?

Describe the bug

I am unsure if this implementation is the same algorithm as the original.

To Reproduce

The entropy is computed differently:

https://github.com/paralleldrive/cuid2/blob/53e246b0919c8123e492e6b6bbab41fe66f4b462/src/index.js#L7-L14

private static String createEntropy(final int length) {
int primeNumber;
final StringBuilder stringBuilder = new StringBuilder(length);
while (stringBuilder.length() < length) {
primeNumber = PRIME_NUMBER_ARRAY[Math.abs(Common.nextIntValue()) % PRIME_NUMBER_ARRAY.length];
stringBuilder.append(Integer.toString(primeNumber * Common.nextIntValue(), NUMBER_BASE));
}
return stringBuilder.toString();


The hash is computed differently:

https://github.com/paralleldrive/cuid2/blob/53e246b0919c8123e492e6b6bbab41fe66f4b462/src/index.js#L31-L35

private static String computeHash(final String content, final int saltLength) {
final String salt = createEntropy(saltLength);
try {
return new BigInteger(MessageDigest.getInstance("SHA3-256").digest((content + salt).getBytes(StandardCharsets.UTF_8)))
.toString(NUMBER_BASE);
} catch (final NoSuchAlgorithmException exception) {
throw new CUIDGenerationException(exception);
}
}


The alphabet is accessed differently:

https://github.com/paralleldrive/cuid2/blob/53e246b0919c8123e492e6b6bbab41fe66f4b462/src/index.js#L37-L39

final char firstLetter = CUIDv2.ALPHABET_ARRAY[Math.abs(Common.nextIntValue()) % CUIDv2.ALPHABET_ARRAY.length];

https://github.com/ai/nanoid?tab=readme-ov-file

random % alphabet is a popular mistake [...] [t]he distribution will not be even; there will be a lower chance for some symbols to appear compared to others.

Please Support Parameterized Length

Is your feature request related to a problem? Please describe.
When the use-case is simply something like username or URL disambiguation where you don't need strong uniqueness guarantees.

Describe the solution you'd like
Parameterized Length in CUID 2 https://github.com/paralleldrive/cuid2#parameterized-length

Describe alternatives you've considered
N/A

Additional context
official javascript example of same

import { init } from '@paralleldrive/cuid2';
const length = 10; // 50% odds of collision after ~51,386,368 ids
const cuid = init({ length });
console.log(cuid()); // nw8zzfaa4v

Cuid standard is deprecated.

Due to security concerns (which also exist in database auto-increment ids, uuid/guid, and most other id standrads), the Cuid standard is now deprecated in favor of Cuid2.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.