You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rw...@apache.org on 2004/12/14 13:39:59 UTC
cvs commit: jakarta-commons-sandbox/id/xdocs index.xml uuid.xml
rwinston 2004/12/14 04:39:59
Modified: id project.xml
id/src/java/org/apache/commons/id/uuid Constants.java
UUID.java
id/src/test/org/apache/commons/id/uuid UUIDTest.java
id/xdocs index.xml uuid.xml
Log:
Added support for UUID generation as specified in the latest UUID draft (http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-04.txt). The only difference is support for SHA-1 encoding when generating UUIDs from a name and namespace.
Revision Changes Path
1.11 +6 -0 jakarta-commons-sandbox/id/project.xml
Index: project.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/id/project.xml,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- project.xml 22 Jul 2004 19:31:16 -0000 1.10
+++ project.xml 14 Dec 2004 12:39:58 -0000 1.11
@@ -69,6 +69,12 @@
</developers>
<contributors>
+ <contributor>
+ <name>Rory Winston</name>
+ <id>rwinston</id>
+ <email>rwinston@eircom.net</email>
+ <organization></organization>
+ </contributor>
</contributors>
<dependencies>
1.3 +11 -1 jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid/Constants.java
Index: Constants.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid/Constants.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Constants.java 14 Jun 2004 12:45:30 -0000 1.2
+++ Constants.java 14 Dec 2004 12:39:59 -0000 1.3
@@ -92,6 +92,16 @@
/** Version four constant for UUID version four of four */
int VERSION_FOUR = 4;
+ /** Version five constant for UUID version five - identical to version 3 */
+ int VERSION_FIVE = 3;
+
+ /** Constants that correspond to the encoding being used, a la
+ * http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-04.txt.
+ * Current legal values are "MD5" and "SHA1"
+ */
+ String MD5_ENCODING = "MD5";
+ String SHA1_ENCODING = "SHA1";
+
//** Exception message constants
/** Message indicating this is not a version one UUID */
String WRONG_VAR_VER_MSG = "Not a ietf variant 2 or version 1 (time-based UUID)";
1.10 +40 -6 jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid/UUID.java
Index: UUID.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid/UUID.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- UUID.java 22 Jul 2004 20:03:25 -0000 1.9
+++ UUID.java 14 Dec 2004 12:39:59 -0000 1.10
@@ -287,8 +287,9 @@
* <ul>
* <li>VERSION_ONE - The time-based version</li>
* <li>VERSION_TWO - DCE Security version, with embedded POSIX UIDs.</li>
- * <li>VERSION_THREE - Name based UUID.</li>
+ * <li>VERSION_THREE - Name based UUID with MD5 hashing.</li>
* <li>VERSION_FOUR - Random based UUID.</li>
+ * <li>VERSION_FIVE - Name based UUID with SHA-1 hashing.</li>
* </ul>
* </p>
* @return the version of the UUID.
@@ -419,25 +420,58 @@
}
/**
- * <p>Returns a new version three UUID given a name and the namespace's UUID.</p>
+ * <p>Returns a new version three (MD5) or five (SHA-1) UUID, using the specified encoding
+ * given a name and the namespace's UUID.</p>
*
* @param name String the name to calculate the UUID for.
* @param namespace UUID assigned to this namespace.
+ * @param encoding The encoding to use, either #{link UUID.MD5_ENCODING} or #{link UUID.SHA1_ENCODING}
* @return a new version three UUID given a name and the namespace's UUID.
*/
- static UUID nameUUIDFromString(String name, UUID namespace) {
+ static UUID nameUUIDFromString(String name, UUID namespace, String encoding) {
byte[] nameAsBytes = name.getBytes();
byte[] concat = new byte[UUID_BYTE_LENGTH + nameAsBytes.length];
System.arraycopy(namespace.getRawBytes(), 0, concat, 0, UUID_BYTE_LENGTH);
System.arraycopy(nameAsBytes, 0, concat, UUID_BYTE_LENGTH, nameAsBytes.length);
- byte[] raw = DigestUtils.md5(concat);
- //Set version
+
+ byte[] raw = null;
+
+ if(encoding.equals(UUID.MD5_ENCODING)) {
+ raw = DigestUtils.md5(concat);
+ }
+ else if(encoding.equals(UUID.SHA1_ENCODING)) {
+ byte[] shaDigest = DigestUtils.sha(concat);
+ // Truncate digest to 16 bytes (SHA-1 returns a 20-byte digest)
+ raw = new byte[16];
+ System.arraycopy(shaDigest, 0, raw, 0, 16);
+ }
+ else {
+ throw new RuntimeException("Unsupported encoding " + encoding);
+ }
+
+
+ //Set version (version 3 and version 5 are identical on a bit-level,
+ //thus we only need ever set one of them
raw[TIME_HI_AND_VERSION_BYTE_6] &= 0x0F;
raw[TIME_HI_AND_VERSION_BYTE_6] |= (UUID.VERSION_THREE << 4);
+
//Set variant
raw[CLOCK_SEQ_HI_AND_RESERVED_BYTE_8] &= 0x3F; //0011 1111
raw[CLOCK_SEQ_HI_AND_RESERVED_BYTE_8] |= 0x80; //1000 0000
return new UUID(raw);
}
+
+ /**
+ * <p>Returns a new version three UUID given a name and the namespace's UUID.</p>
+ *
+ * @param name String the name to calculate the UUID for.
+ * @param namespace UUID assigned to this namespace.
+ * @return a new version three UUID given a name and the namespace's UUID.
+ *
+ */
+ static UUID nameUUIDFromString(String name, UUID namespace) {
+ return nameUUIDFromString(name, namespace, UUID.MD5_ENCODING);
+ }
+
}
1.7 +32 -1 jakarta-commons-sandbox/id/src/test/org/apache/commons/id/uuid/UUIDTest.java
Index: UUIDTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/id/src/test/org/apache/commons/id/uuid/UUIDTest.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- UUIDTest.java 14 Jun 2004 12:54:36 -0000 1.6
+++ UUIDTest.java 14 Dec 2004 12:39:59 -0000 1.7
@@ -331,6 +331,35 @@
assertEquals("3d813cbb-47fb-32ba-91df-831e1593ac29", known.toString());
}
+ /**
+ * Test the static #{link UUID.nameUUIDFromString} method, with explicit SHA-1 encoding
+ * as specified in version 4 of the UUID draft.
+ */
+ public void testNameUUIDFromStringSha1() throws Exception {
+ //UUID assigned to URL Namespace
+ UUID ns = UUID.fromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8");
+ //UUID assigned to ISO OID
+ UUID nsAlt = UUID.fromString("6ba7b812-9dad-11d1-80b4-00c04fd430c8");
+
+ String name = "www.apache.org";
+ String nameAlt = "cvs.apache.org";
+ UUID test1 = UUID.nameUUIDFromString(name, ns, UUID.SHA1_ENCODING);
+
+ assertEquals(Constants.VARIANT_IETF_DRAFT, test1.variant());
+ assertEquals(Constants.VERSION_FIVE, test1.version());
+
+ //Assert not same - same name from different namespace
+ UUID test2 = UUID.nameUUIDFromString(name, nsAlt, UUID.SHA1_ENCODING);
+ assertTrue(!test2.equals(test1));
+ //Assert not same - same namespace different names
+ UUID test3 = UUID.nameUUIDFromString(nameAlt, ns, UUID.SHA1_ENCODING);
+ assertTrue(!test3.equals(test1));
+ //Assert equals different UUID instance from same name, namespace
+ UUID test4 = UUID.nameUUIDFromString(name, ns, UUID.SHA1_ENCODING);
+ assertTrue(test4.equals(test1));
+
+ }
+
/**
* Test the toString of UUID
@@ -393,10 +422,12 @@
UUID v2 = new UUID("3051a8d7-aea7-2801-e0bf-bc539dd60cf3"); //Version two 0x28 = 0010 1000
UUID v3 = new UUID("3051a8d7-aea7-3801-e0bf-bc539dd60cf3"); //Version three 0x38 = 0011 1000
UUID v4 = new UUID("3051a8d7-aea7-4801-e0bf-bc539dd60cf3"); //Version four 0x48 = 0100 1000
+ UUID v5 = new UUID("3051a8d7-aea7-3801-e0bf-bc539dd60cf3"); //Version five 0x38 = 0011 1000
assertEquals(UUID.VERSION_ONE, v1.version());
assertEquals(UUID.VERSION_TWO, v2.version());
assertEquals(UUID.VERSION_THREE, v3.version());
assertEquals(UUID.VERSION_FOUR, v4.version());
+ assertEquals(UUID.VERSION_FIVE, v5.version());
}
/**
1.6 +2 -2 jakarta-commons-sandbox/id/xdocs/index.xml
Index: index.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/id/xdocs/index.xml,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- index.xml 31 May 2004 06:37:43 -0000 1.5
+++ index.xml 14 Dec 2004 12:39:59 -0000 1.6
@@ -81,7 +81,7 @@
time period.</td></tr>
<tr><td>
<a href="uuid.html">UUID Generators</a></td><td>Generates universally Unique Identifiers
- based on the <a href="http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-03.txt">
+ based on the <a href="http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-04.txt">
IETF Draft Uuid Specification.</a>
</td></tr>
</table>
1.3 +12 -2 jakarta-commons-sandbox/id/xdocs/uuid.xml
Index: uuid.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/id/xdocs/uuid.xml,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- uuid.xml 31 May 2004 06:37:59 -0000 1.2
+++ uuid.xml 14 Dec 2004 12:39:59 -0000 1.3
@@ -25,8 +25,8 @@
<section name="Overview">
<p>
A Universally Unique Identifier (UUID) is a 128-bit identifier described in the
- <a href="http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-03.txt">IETF Draft Uuid Specification</a>.
- Generators for version 1 and version 4 UUID's are provided. The value held in a UUID is represented
+ <a href="http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-04.txt">IETF Draft Uuid Specification</a>.
+ Generators for versions 1,3,4 and 5 UUID's are provided. The value held in a UUID is represented
by a specific hexadecimal format of the binary fields. An example UUID string representation is:
F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6. A cautionary note: there is no standard regarding binary
representation of a UUID other than its string format.
@@ -37,6 +37,16 @@
The version 4 UUID is UUID based on random bytes. We fill the 128-bits with random bits (6 of the
bits are correspondingly set to flag the version and variant of the UUID). No special configuration
or implementation decisions are required to generate version 4 UUID's.
+</p>
+</section>
+<section name="UUID version 3">
+<p>
+ Version 3 UUIDs are initialized using a name, a namespace, and the MD5 hashing algorithm.
+</p>
+</section>
+<section name="UUID version 5">
+ <p>
+ Version 5 UUIDs are initialized using a name, a namespace, and the SHA-1 hashing algorithm.
</p>
</section>
<section name="UUID version 1">
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [id] Remove dependency on codec?
Posted by Phil Steitz <ph...@steitz.com>.
+1
-Phil
Rory Winston wrote:
> If there are any [id] devs watching, do they think it might be
> worthwhile removing the explicit dependency on [codec]? The only
> portions of codec which are used in [id] are the Hex encoding and the
> MD5/SHA-1 encoding routines. The hex encoding is trivial and could be
> imported to the [id] codebase, and the MD5/SHA-1 routines just delegate
> to the JDK methods anyways, so it seems a bit pointless.
>
> Anyone got any objections?
>
> Cheers,
> Rory
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [id] Remove dependency on codec?
Posted by Stephen Colebourne <sc...@btopenworld.com>.
+1
Stephen
--- Rory Winston <rw...@eircom.net> wrote:
> If there are any [id] devs watching, do they think
> it might be
> worthwhile removing the explicit dependency on
> [codec]? The only
> portions of codec which are used in [id] are the Hex
> encoding and the
> MD5/SHA-1 encoding routines. The hex encoding is
> trivial and could be
> imported to the [id] codebase, and the MD5/SHA-1
> routines just delegate
> to the JDK methods anyways, so it seems a bit
> pointless.
>
> Anyone got any objections?
>
> Cheers,
> Rory
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> commons-dev-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
[id] Remove dependency on codec?
Posted by Rory Winston <rw...@eircom.net>.
If there are any [id] devs watching, do they think it might be
worthwhile removing the explicit dependency on [codec]? The only
portions of codec which are used in [id] are the Hex encoding and the
MD5/SHA-1 encoding routines. The hex encoding is trivial and could be
imported to the [id] codebase, and the MD5/SHA-1 routines just delegate
to the JDK methods anyways, so it seems a bit pointless.
Anyone got any objections?
Cheers,
Rory
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org