Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / media / encrypted-media / encrypted-media-utils.js
1 function stringToUint8Array(str)
2 {
3     var result = new Uint8Array(str.length);
4     for(var i = 0; i < str.length; i++) {
5         result[i] = str.charCodeAt(i);
6     }
7     return result;
8 }
9
10 // For Clear Key, MediaKeySession.update() takes a JSON Web Key (JWK) Set,
11 // which contains a set of cryptographic keys represented by JSON. These helper
12 // functions help wrap raw keys into a JWK set.
13 // See:
14 // https://dvcs.w3.org/hg/html-media/raw-file/tip/encrypted-media/encrypted-media.html#simple-decryption-clear-key
15 // http://tools.ietf.org/html/draft-ietf-jose-json-web-key
16
17 // Encodes data into base64 string without trailing '='.
18 function base64Encode(data)
19 {
20     var result = btoa(String.fromCharCode.apply(null, data));
21     return result.replace(/=+$/g, '');
22 }
23
24 // Creates a JWK from raw key ID and key.
25 function createJWK(keyId, key)
26 {
27     var jwk = "{\"kty\":\"oct\",\"kid\":\"";
28     jwk += base64Encode(keyId);
29     jwk += "\",\"k\":\"";
30     jwk += base64Encode(key);
31     jwk += "\"}";
32     return jwk;
33 }
34
35 // Creates a JWK Set from multiple JWKs.
36 function createJWKSet()
37 {
38     var jwkSet = "{\"keys\":[";
39     for (var i = 0; i < arguments.length; i++) {
40         if (i != 0)
41             jwkSet += ",";
42         jwkSet += arguments[i];
43     }
44     jwkSet += "]}";
45     return jwkSet;
46 }
47