@@ -110,8 +110,9 @@ export function isAssertionConfig(obj: unknown): obj is AssertionConfig {
110
110
*/
111
111
export async function verify (
112
112
thiz : Assertion ,
113
- aggregateHash : string ,
114
- key : AssertionKey
113
+ aggregateHash : Uint8Array ,
114
+ key : AssertionKey ,
115
+ isLegacyTDF : boolean
115
116
) : Promise < void > {
116
117
let payload : AssertionPayload ;
117
118
try {
@@ -126,14 +127,23 @@ export async function verify(
126
127
127
128
// Get the hash of the assertion
128
129
const hashOfAssertion = await hash ( thiz ) ;
129
- const combinedHash = aggregateHash + hashOfAssertion ;
130
- const encodedHash = base64 . encode ( combinedHash ) ;
131
130
132
131
// check if assertionHash is same as hashOfAssertion
133
132
if ( hashOfAssertion !== assertionHash ) {
134
133
throw new IntegrityError ( 'Assertion hash mismatch' ) ;
135
134
}
136
135
136
+ let encodedHash : string ;
137
+ if ( isLegacyTDF ) {
138
+ const aggregateHashAsStr = new TextDecoder ( 'utf-8' ) . decode ( aggregateHash ) ;
139
+ const combinedHash = aggregateHashAsStr + hashOfAssertion ;
140
+ encodedHash = base64 . encode ( combinedHash ) ;
141
+ } else {
142
+ const combinedHash = concatenateUint8Arrays ( aggregateHash ,
143
+ new Uint8Array ( hex . decodeArrayBuffer ( assertionHash ) ) ) ;
144
+ encodedHash = base64 . encodeArrayBuffer ( combinedHash ) ;
145
+ }
146
+
137
147
// check if assertionSig is same as encodedHash
138
148
if ( assertionSig !== encodedHash ) {
139
149
throw new IntegrityError ( 'Failed integrity check on assertion signature' ) ;
@@ -144,7 +154,7 @@ export async function verify(
144
154
* Creates an Assertion object with the specified properties.
145
155
*/
146
156
export async function CreateAssertion (
147
- aggregateHash : string ,
157
+ aggregateHash : Uint8Array ,
148
158
assertionConfig : AssertionConfig
149
159
) : Promise < Assertion > {
150
160
if ( ! assertionConfig . signingKey ) {
@@ -162,8 +172,8 @@ export async function CreateAssertion(
162
172
} ;
163
173
164
174
const assertionHash = await hash ( a ) ;
165
- const combinedHash = aggregateHash + assertionHash ;
166
- const encodedHash = base64 . encode ( combinedHash ) ;
175
+ const combinedHash = concatenateUint8Arrays ( aggregateHash , new Uint8Array ( hex . decodeArrayBuffer ( assertionHash ) ) ) ;
176
+ const encodedHash = base64 . encodeArrayBuffer ( combinedHash ) ;
167
177
168
178
return await sign ( a , assertionHash , encodedHash , assertionConfig . signingKey ) ;
169
179
}
@@ -189,3 +199,13 @@ export type AssertionVerificationKeys = {
189
199
DefaultKey ?: AssertionKey ;
190
200
Keys : Record < string , AssertionKey > ;
191
201
} ;
202
+
203
+ function concatenateUint8Arrays ( array1 : Uint8Array , array2 : Uint8Array ) : Uint8Array {
204
+ const combinedLength = array1 . length + array2 . length ;
205
+ const combinedArray = new Uint8Array ( combinedLength ) ;
206
+
207
+ combinedArray . set ( array1 , 0 ) ;
208
+ combinedArray . set ( array2 , array1 . length ) ;
209
+
210
+ return combinedArray ;
211
+ }
0 commit comments