|
1 /* |
|
2 * Copyright (c) 2002-2005 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 // INCLUDE FILES |
|
26 #include <hash.h> |
|
27 #include <e32math.h> |
|
28 #include <f32file.h> // TVolumeInfo |
|
29 #include "senguidgen.h" |
|
30 #include "sendebug.h" |
|
31 #include "senlogger.h" |
|
32 |
|
33 CSenGuidGen::CSenGuidGen() |
|
34 { |
|
35 TTime homeTime; |
|
36 homeTime.HomeTime(); |
|
37 iSeed = homeTime.Int64(); |
|
38 } |
|
39 |
|
40 void CSenGuidGen::ConstructL() |
|
41 { |
|
42 TLSLOG_L(KSenUtilsLogChannel, KMinLogLevel, "CSenGuidGen::ConstructL"); |
|
43 // Get host name |
|
44 //ipHostId = _L8("localhost").AllocL(); |
|
45 ipHostId = HBufC8::NewL( KSenUuidMaxHostIdLength ); |
|
46 TPtr8 hostID = ipHostId->Des(); |
|
47 |
|
48 TInt error(KErrNone); |
|
49 |
|
50 |
|
51 RFs rfs; |
|
52 User::LeaveIfError(rfs.Connect()); |
|
53 #ifndef __WINS__ // only required in RELEASE builds |
|
54 TLSLOG_L(KSenUtilsLogChannel, KNormalLogLevel, "CSenGuidGen::ConstructL - about to call RFs.GetMediaSerialNumber()"); |
|
55 TMediaSerialNumber mediaID; |
|
56 error = rfs.GetMediaSerialNumber(mediaID, EDriveC); |
|
57 TLSLOG_FORMAT((KSenUtilsLogChannel, KMinLogLevel, _L8("CSenGuidGen::ConstructL - GetMediaSerialNumber() returned: %d"), error)); |
|
58 if( !error ) |
|
59 { |
|
60 hostID.Append(mediaID); |
|
61 } |
|
62 #else // WINS (emulator) environment |
|
63 TLSLOG_L(KSenUtilsLogChannel, KNormalLogLevel, "CSenGuidGen::ConstructL - about to call RFs.Volume()"); |
|
64 TVolumeInfo info; |
|
65 error = rfs.Volume(info); |
|
66 TLSLOG_FORMAT((KSenUtilsLogChannel, KMinLogLevel, _L8("CSenGuidGen::ConstructL - Volume() returned: %d"), error)); |
|
67 if( !error ) |
|
68 { |
|
69 hostID.AppendNum(info.iUniqueID); |
|
70 } |
|
71 #endif |
|
72 rfs.Close(); |
|
73 |
|
74 if( error ) |
|
75 { |
|
76 hostID.Append( _L8("localhost") ); |
|
77 } |
|
78 TLSLOG_L(KSenUtilsLogChannel, KMinLogLevel, "CSenGuidGen::ConstructL - host ID:"); |
|
79 TLSLOG(KSenUtilsLogChannel, KMinLogLevel, hostID); |
|
80 // Create hash generator |
|
81 ipMd5HashGen = CMD5::NewL(); |
|
82 } |
|
83 |
|
84 CSenGuidGen::~CSenGuidGen() |
|
85 { |
|
86 delete ipMd5HashGen; |
|
87 delete ipHostId; |
|
88 } |
|
89 |
|
90 EXPORT_C CSenGuidGen* CSenGuidGen::NewLC() |
|
91 { |
|
92 CSenGuidGen* pNew = new (ELeave) CSenGuidGen; |
|
93 CleanupStack::PushL(pNew); |
|
94 pNew->ConstructL(); |
|
95 return pNew; |
|
96 } |
|
97 |
|
98 EXPORT_C CSenGuidGen* CSenGuidGen::NewL() |
|
99 { |
|
100 CSenGuidGen* pNew = NewLC(); |
|
101 CleanupStack::Pop(); // pNew; |
|
102 return pNew; |
|
103 } |
|
104 |
|
105 EXPORT_C HBufC* CSenGuidGen::GetRandomGuidLC() |
|
106 { |
|
107 #ifdef _UNICODE |
|
108 return GetRandomGuid16LC(); |
|
109 #else |
|
110 return GetRandomGuid8LC(); |
|
111 #endif |
|
112 } |
|
113 |
|
114 |
|
115 EXPORT_C HBufC8* CSenGuidGen::GetRandomGuid8LC() |
|
116 { |
|
117 TPtrC8 hash = GetRandomHash(); |
|
118 HBufC8* pResult = HBufC8::NewLC(hash.Length() * KSenUuidHexWidth + KSenUuidNumOfSlashes + KSenUuidPrefix().Length()); |
|
119 TPtr8 result = pResult->Des(); |
|
120 |
|
121 // Ensure that resulting uuid is a URI as well as an NCName |
|
122 result.Append(KSenUuidPrefix) ; |
|
123 |
|
124 for (TInt i = 0; i < hash.Length(); i++) |
|
125 { |
|
126 if ((i == 4) || (i == 6) || (i == 8) || (i == 10)) // four slashes |
|
127 { |
|
128 result.Append('-'); |
|
129 } |
|
130 result.AppendNumFixedWidth(hash[i], EHex, KSenUuidHexWidth); // hash[i] as hex!, doubles the hash length |
|
131 } |
|
132 result.UpperCase(); |
|
133 return pResult; |
|
134 } |
|
135 |
|
136 |
|
137 EXPORT_C HBufC16* CSenGuidGen::GetRandomGuid16LC() |
|
138 { |
|
139 TPtrC8 hash = GetRandomHash(); |
|
140 HBufC16* pResult = HBufC16::NewLC(hash.Length() * KSenUuidHexWidth + KSenUuidNumOfSlashes + KSenUuidPrefix().Length()); |
|
141 TPtr16 result = pResult->Des(); |
|
142 |
|
143 // Ensure that resulting uuid is a URI as well as an NCName |
|
144 |
|
145 TBuf16<KSenUuidPrefixLength> uuidPrefix16; // KSenUuidPrefixLength == KSenUuidPrefix().Length() |
|
146 |
|
147 uuidPrefix16.Copy(KSenUuidPrefix); // OK, since there are no Unicode characters in KSenUuidPrefix |
|
148 result.Append(uuidPrefix16); |
|
149 |
|
150 for (TInt i = 0; i < hash.Length(); i++) |
|
151 { |
|
152 if ((i == 4) || (i == 6) || (i == 8) || (i == 10)) // four slashes |
|
153 { |
|
154 result.Append('-'); |
|
155 } |
|
156 result.AppendNumFixedWidth(hash[i], EHex, KSenUuidHexWidth); // hash[i] as hex!, doubles the hash length |
|
157 } |
|
158 result.UpperCase(); |
|
159 return pResult; |
|
160 } |
|
161 |
|
162 TPtrC8 CSenGuidGen::GetRandomHash() |
|
163 { |
|
164 TBuf8<KSenUuidMaxHashLength> valueBeforeMd5; |
|
165 |
|
166 // Append host id |
|
167 valueBeforeMd5.Append(*ipHostId); |
|
168 valueBeforeMd5.Append(':'); |
|
169 |
|
170 // Append current system time |
|
171 TTime homeTime; |
|
172 homeTime.HomeTime(); |
|
173 valueBeforeMd5.AppendNum(homeTime.Int64()); |
|
174 valueBeforeMd5.Append(':'); |
|
175 |
|
176 // Append random number |
|
177 valueBeforeMd5.AppendNum(Math::Rand(iSeed)); |
|
178 |
|
179 // Calculate hash |
|
180 TPtrC8 hash = ipMd5HashGen->Hash(valueBeforeMd5); |
|
181 return hash; |
|
182 } |
|
183 |
|
184 // End of File |