2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <arpa/inet.h>
21 #include "ext4_utils.h"
25 /* Definition from RFC-4122 */
29 u16 time_hi_and_version
;
36 static void sha1_hash(const char *namespace, const char *name
,
37 unsigned char sha1
[SHA1_DIGEST_LENGTH
])
41 SHA1Update(&ctx
, (const u8
*)namespace, strlen(namespace));
42 SHA1Update(&ctx
, (const u8
*)name
, strlen(name
));
43 SHA1Final(sha1
, &ctx
);
46 void generate_uuid(const char *namespace, const char *name
, u8 result
[16])
48 unsigned char sha1
[SHA1_DIGEST_LENGTH
];
49 struct uuid
*uuid
= (struct uuid
*)result
;
51 sha1_hash(namespace, name
, (unsigned char*)sha1
);
52 memcpy(uuid
, sha1
, sizeof(struct uuid
));
54 uuid
->time_low
= ntohl(uuid
->time_low
);
55 uuid
->time_mid
= ntohs(uuid
->time_mid
);
56 uuid
->time_hi_and_version
= ntohs(uuid
->time_hi_and_version
);
57 uuid
->time_hi_and_version
&= 0x0FFF;
58 uuid
->time_hi_and_version
|= (5 << 12);
59 uuid
->clk_seq_hi_res
&= ~(1 << 6);
60 uuid
->clk_seq_hi_res
|= 1 << 7;