curious1
curious1

Reputation: 14727

How can I generate a string similar to YouTube's video identifier string in Java?

Edits: I reworded this question.

I notice that at YouTube, each video has a unique string such as 1cru2fzUlEc to identify itself.

I am wondering how I can do the same thing efficiently in Java.

Thanks for any input.

Cheers!

Upvotes: 1

Views: 693

Answers (3)

Paul Henry
Paul Henry

Reputation: 375

with apache commons-lang

import org.apache.commons.lang.RandomStringUtils;

public static final int ID_LENGTH = 11;

public String generateUniqueId() {
    return RandomStringUtils.randomAlphanumeric(ID_LENGTH);
}

Upvotes: 3

korius
korius

Reputation: 359

As Patashu already pointed out: These strings are not encrypted they are simply unique identifiers which cannot be guessed or calculated.

This can be achieved in Java using the UUID implementation. These UUIDs are longer than youtube's but the principle is the same.

The security of these UUIDs should be good enough for almost all occasions, as has been discussed here.

Upvotes: 3

Patashu
Patashu

Reputation: 21783

Sites such as youtube don't 'encrypt' the identifier of a video. When the video is made it generates a random string for it, and that random string (after making sure it is unique) IS the video's identifier.

Upvotes: 2

Related Questions