Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/account/ssh-keys/fingerprint.ts
1503 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// Adapted from https://github.com/bahamas10/node-ssh-fingerprint
7
8
import md5 from "md5";
9
10
// add colons, 'hello' => 'he:ll:o'
11
const colons = (s: string) => s.replace(/(.{2})(?=.)/g, "$1:");
12
13
export function compute_fingerprint(pub: string | undefined): string {
14
if (pub == null) {
15
throw new Error("No valid SSH key value");
16
}
17
const pubbuffer = Buffer.from(pub, "base64");
18
const key = md5(
19
new Uint8Array(
20
pubbuffer.buffer,
21
pubbuffer.byteOffset,
22
pubbuffer.byteLength,
23
),
24
);
25
return colons(key);
26
}
27
28