Skip to content

Instantly share code, notes, and snippets.

@morloy
Last active September 12, 2018 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morloy/845fba86e1c1d56b32aae01d75a98a40 to your computer and use it in GitHub Desktop.
Save morloy/845fba86e1c1d56b32aae01d75a98a40 to your computer and use it in GitHub Desktop.
const hexToBytes = (hex: string): Array<number> => {
const bytes = [];
for (let c = 0; c < hex.length; c += 2) {
bytes.push(parseInt(hex.substr(c, 2), 16));
}
return bytes;
};
export const SHA256 = async (doc: ArrayBuffer): Promise<string> => (
crypto ?
crypto.createHash('sha256').update(doc).digest('hex') :
Buffer.from(await window.crypto.subtle.digest('SHA-256', doc)).toString('hex')
);
export const getOtsFromCertificate = (certificate: string) =>
OTS.DetachedTimestampFile.fromHash(
new OTS.Ops.OpSHA256(),
hexToBytes(await SHA256(Buffer.from(certificate, 'base64'))),
);
export const encodeOts = (ots: Object): string =>
Buffer.from(ots.serializeToBytes()).toString('base64');
export const decodeOts = (ots: string): Object =>
OTS.DetachedTimestampFile.deserialize(Buffer.from(ots, 'base64'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment