Hi. I’m trying to calculate signature for Twitter API, so I wanna calculate HmacSHA1 without any libraries.
I wrote this function, calcHmacSHA1()
with Web Crypto API.
// calcHmacSHA1
async function calcHmacSHA1(base, key){
let wv = new WebView()
let html = `
<script>
async function main(BASE, KEY, CALLBACK){
const te = new TextEncoder('utf-8');
const cryptoKey = await crypto.subtle.importKey(
'raw',
te.encode(KEY),
{name: 'HMAC', hash: {name: 'SHA-1'}},
false,
['sign']
);
const signature = await crypto.subtle.sign(
'HMAC',
cryptoKey,
te.encode(BASE)
);
const buff = new Uint8Array(signature);
const str = btoa(String.fromCharCode(...buff))
return str;
};
</script>
`;
let js = `main('${base}', '${key}').then(completion);`;
await wv.loadHTML(html);
let result = await wv.evaluateJavaScript(js, true)
return result;
};
let key = 'MyKey';
let base = 'MyMessage';
console.log(await calcHmacSHA1(base, key));
Log:
Error: Failed evaluating JavaScript with error: JavaScript execution returned a result of an unsupported type
Anyone know how to solve it?