From 61a4347a6458f68263fd7400e757a616226ee91f Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Tue, 28 May 2019 14:10:12 +0100 Subject: [PATCH] PrettyFingerprint: add a widget for pretty showing of fingerprints. --- src/widgets/PrettyFingerprint.tsx | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/widgets/PrettyFingerprint.tsx diff --git a/src/widgets/PrettyFingerprint.tsx b/src/widgets/PrettyFingerprint.tsx new file mode 100644 index 0000000..b05b648 --- /dev/null +++ b/src/widgets/PrettyFingerprint.tsx @@ -0,0 +1,48 @@ +import * as React from 'react'; +import * as sjcl from 'sjcl'; + +import { byte, base64 } from '../api/Helpers'; + +function byteArray4ToNumber(bytes: byte[], offset: number) { + // tslint:disable:no-bitwise + return ( + ((bytes[offset + 0] & 0xff) * (1 << 24)) + + ((bytes[offset + 1] & 0xff) * (1 << 16)) + + ((bytes[offset + 2] & 0xff) * (1 << 8)) + + ((bytes[offset + 3] & 0xff)) + ); +} + +function getEncodedChunk(publicKey: byte[], offset: number) { + const chunk = byteArray4ToNumber(publicKey, offset) % 100000; + return chunk.toString().padStart(5, '0'); +} + +interface PropsType { + publicKey: base64; +} + +class PrettyFingerprint extends React.PureComponent { + public render() { + const fingerprint = sjcl.codec.bytes.fromBits( + sjcl.hash.sha256.hash(sjcl.codec.base64.toBits(this.props.publicKey)) + ); + + const spacing = ' '; + const prettyPublicKey = + getEncodedChunk(fingerprint, 0) + spacing + + getEncodedChunk(fingerprint, 4) + spacing + + getEncodedChunk(fingerprint, 8) + spacing + + getEncodedChunk(fingerprint, 12) + '\n' + + getEncodedChunk(fingerprint, 16) + spacing + + getEncodedChunk(fingerprint, 20) + spacing + + getEncodedChunk(fingerprint, 24) + spacing + + getEncodedChunk(fingerprint, 28); + + return ( +
{prettyPublicKey}
+ ); + } +} + +export default PrettyFingerprint;