Last active
September 12, 2018 17:45
-
-
Save morloy/845fba86e1c1d56b32aae01d75a98a40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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