All Projects → Devskiller → friendly-id

Devskiller / friendly-id

Licence: Apache-2.0 license
Java Friendly Id for UUID

Programming Languages

java
68154 projects - #9 most used programming language
groovy
2714 projects

Projects that are alternatives of or similar to friendly-id

Butterfly
分布式ID生成器框架:超高性能的发号器框架。通过引入多种新的方案,彻底解决雪花算法的时间回拨等问题,并将雪花算法原生QPS提高最少十几~二十倍
Stars: ✭ 111 (-35.84%)
Mutual labels:  uuid, unique-id, unique-identifier
sno
Compact, sortable and fast unique IDs with embedded metadata.
Stars: ✭ 77 (-55.49%)
Mutual labels:  uuid, unique-id
python-ulid
ULID implementation for Python
Stars: ✭ 177 (+2.31%)
Mutual labels:  uuid, unique-id
id-mask
IDMask is a Java library for masking internal ids (e.g. from your DB) when they need to be published to hide their actual value and to prevent forging. It has support optional randomisation has a wide support for various Java types including long, UUID and BigInteger. This library bases its security on strong cryptographic primitives.
Stars: ✭ 39 (-77.46%)
Mutual labels:  uuid, jackson
fuuid
Functional UUIDs for Python.
Stars: ✭ 145 (-16.18%)
Mutual labels:  uuid, unique-id
order-id
Unique order id generator
Stars: ✭ 46 (-73.41%)
Mutual labels:  unique-id, unique-identifier
ulid-creator
A Java library for generating Universally Unique Lexicographically Sortable Identifiers (ULID)
Stars: ✭ 38 (-78.03%)
Mutual labels:  uuid, unique-id
Ksuid
K-Sortable Globally Unique IDs
Stars: ✭ 3,202 (+1750.87%)
Mutual labels:  uuid, unique-id
Uid Promise
Creates a cryptographically strong UID
Stars: ✭ 179 (+3.47%)
Mutual labels:  uuid
Uuid
Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.
Stars: ✭ 3,237 (+1771.1%)
Mutual labels:  uuid
Icicle
A distributed, k-sortable unique ID generation system using Redis and Lua.
Stars: ✭ 159 (-8.09%)
Mutual labels:  uuid
Id Generator
生成带校验码的卡号、19位的Long ID、不大于22位的短UUID、短卡号、激活码、数字加密、付款码。分布式、基于内存、安全可靠、性能高。
Stars: ✭ 180 (+4.05%)
Mutual labels:  uuid
Ulid
Universally Unique Lexicographically Sortable Identifier implementation for Ruby
Stars: ✭ 253 (+46.24%)
Mutual labels:  uuid
Lua Resty Jit Uuid
Fast and dependency-free UUID library for LuaJIT/ngx_lua
Stars: ✭ 169 (-2.31%)
Mutual labels:  uuid
apina
Tool for creating client-side TypeScript code from server-side APIs
Stars: ✭ 27 (-84.39%)
Mutual labels:  jackson
Zkudid
Generate and save permanent UDID with IDFV and keychain in iOS device.
Stars: ✭ 159 (-8.09%)
Mutual labels:  uuid
Uuid
Kotlin Multiplatform UUID
Stars: ✭ 146 (-15.61%)
Mutual labels:  uuid
catnap
Partial JSON response framework for RESTful web services
Stars: ✭ 55 (-68.21%)
Mutual labels:  jackson
Ulid
Universally Unique Lexicographically Sortable Identifier (ULID) in Python 3
Stars: ✭ 216 (+24.86%)
Mutual labels:  uuid
Python Wechat Itchat
微信机器人,基于Python itchat接口功能实例展示:01-itchat获取微信好友或者微信群分享文章、02-itchat获取微信公众号文章、03-itchat监听微信公众号发送的文章、04 itchat监听微信群或好友撤回的消息、05 itchat获得微信好友信息以及表图对比、06 python打印出微信被删除好友、07 itchat自动回复好友、08 itchat微信好友个性签名词云图、09 itchat微信好友性别比例、10 微信群或微信好友撤回消息拦截、11 itchat微信群或好友之间转发消息
Stars: ✭ 216 (+24.86%)
Mutual labels:  uuid

Build Status Maven Central Coverage Status

FriendlyID (Java, Swift, Rust, Go)

What is the FriendlyID library?

The FriendlyID library converts a given UUID (with 36 characters) to a URL-friendly ID (a "FriendlyID") which is based on Base62 (with a maximum of 22 characters), as in the example below:

UUID                                        Friendly ID

c3587ec5-0976-497f-8374-61e0c2ea3da5   ->   5wbwf6yUxVBcr48AMbz9cb
|                                           |                              
36 characters                               22 characters or less

In addition, this library allows to:

  • convert from a FriendlyID back to the original UUID; and
  • create a new, random FriendlyID

Why use a FriendlyID?

Universal Unique IDs (UUIDs) provide a non-sequential and unique identifier that can be generated separately from the source database. As a result, it is not possible to guess either the previous or next identifier. That's great, but, to achieve this level of security, a UUID is long (128 bits long) and looks ugly (36 alphanumeric characters including four hyphens which are added to make it easier to read the UUID), as in this example: 123e4567-e89b-12d3-a456-426655440000.

Such a format is:

  • difficult to read (especially if it is part of a URL)
  • difficult to remember
  • cannot be copied with just two mouse-clicks (you have to select manually the start and end positions)
  • can easily become broken across lines when it is copied, pasted, edited, or sent.

Our FriendlyID Java library solves these problems by converting a given UUID using Base62 with alphanumeric characters in the range [0-9A-Za-z] into a FriendlyId which consists of a maximum of 22 characters (but in fact often contains fewer characters).

Supported languages

Curently FriendlyId supports Java (this project) and

Tools

There are available CLI converters for many platforms.

Use cases

Basic (returning a user in a database)

Let us assume that a method in the controller for returning users requires the relevant UUID in order to find a given user in a database, as in this example:

@GetMapping("/users/{userId}") 
public User getUser(@PathVariable UUID userId) {
        [implementation deleted]
}

Without using the Friendly ID library, you could access a given user as follows:

curl http://localhost:8080/users/c3587ec5-0976-497f-8374-61e0c2ea3da5

After adding the FriendlyID library, the controller method itself does not change, but you would be able to access a given user using the relevant FriendlyID as follows:

curl http://localhost:8080/users/5wbwf6yUxVBcr48AMbz9cb

In addition, if a given document returned by such a method contains objects of type UUID, those IDs will also be shortened into FriendlyID format.

Advanced (Optimizing testing)

The FriendlyID library makes it possible to define for UUIDs values which are easy to read. By using names instead of hard-to-remember UUIDs, you can write much simpler tests for your code, for example:

@Test 
public void shouldGetUser() { 
    mockMvc.perform(get("/users/{userId}", "John")) 
        .andExpect(status().isOk()) 
        .andExpect(content().contentType("application/json")) 
        .andExpect(jsonPath("$.uuid", is("John"))); 
} 

In the above example, the variable "John" is decoded by the library to the correct UUID, in this case, 00000000-0000-0000-0000-000000a69efb. In this way, you can give a variable in a test class a truly meaningful value and, as a result, an assertion which refers to that variable becomes exceptionally easy to understand in your test program.

FriendlyID library

Dependencies

<dependency>
    <groupId>com.devskiller.friendly-id</groupId>
    <artifactId>friendly-id</artifactId>
    <version>1.1.0</version>
</dependency>

Usage

FriendlyId.createFriendlyId();

This creates a new, random FriendlyID, for example: 5wbwf6yUxVBcr48AMbz9cb

FriendlyId.toFriendlyId(UUID.fromString("c3587ec5-0976-497f-8374-61e0c2ea3da5"));

This converts a UUID in the form of a string to a FriendlyID, for example: 5wbwf6yUxVBcr48AMbz9cb

FriendlyId.toUuid("5wbwf6yUxVBcr48AMbz9cb");

This converts a FriendlyID to its UUID, for example: c3587ec5-0976-497f-8374-61e0c2ea3da5

Notes

  • As every UUID is a 128-bit number, a FriendlyID can also store only a 128-bit number.
  • If a FriendlyID has any leading zeros, those leading zeros are ignored - for example, 00cafe is treated as cafe.

Integrations

Spring Boot integration

The FriendlyID library includes a Spring configuration to make it easy to add shorter IDs to an application. With a typical application based on Spring Boot, for your controllers to be able to use FriendlyIDs when communicating with the outside world, just add one new starter dependency as follows:

<dependency>
    <groupId>com.devskiller.friendly-id</groupId>
    <artifactId>friendly-id-spring-boot-starter</artifactId>
    <version>1.1.0</version>
</dependency>

Let us assume that you'll use this sample application:

@SpringBootApplication
@RestController
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @GetMapping("/bars/{bar}")
    public Bar getBar(@PathVariable UUID bar) {
        return new Bar(UUID.randomUUID());
    }

    @Value
    class Bar {
        private final UUID id;
    }
}  

This command: curl http://localhost:8080/bars/5fD1KwsxRcGhBqWNju0jzt

will result in the following output:

{"id":"52OMXhWiAqUWwII0c97Svl"}

In this case, Bar is a POJO class which is converted by Spring MVC to a JSON document. This Bar object has one field of type UUID, and this field is output to the JSON document as a FriendlyID instead of a UUID. Although the application uses the relevant UUID internally, from an external point of view, only the FriendlyID is visible.

Jackson integration

First, add the following Jackson module dependency:

<dependency>
    <groupId>com.devskiller.friendly-id</groupId>
    <artifactId>friendly-id-jackson-datatype</artifactId>
    <version>1.1.0</version>
</dependency>

Then register the FriendlyIdModule module as follows:

ObjectMapper mapper = new ObjectMapper()
   .registerModule(new FriendlyIdModule());

Contributing

Thinking of helping us out? We invite you to take a look at:

License

The project is licensed under the Apache 2.0 license. For further details, please see the License page.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].