Refactor ssh key generation function to a new ssh utils file
This commit is contained in:
parent
788499bcf2
commit
9625939aa5
2 changed files with 33 additions and 33 deletions
|
|
@ -14,44 +14,12 @@ import { SecretInput } from "../../../../components/ui/secret-input";
|
||||||
import { Textarea } from "../../../../components/ui/textarea";
|
import { Textarea } from "../../../../components/ui/textarea";
|
||||||
import { Switch } from "../../../../components/ui/switch";
|
import { Switch } from "../../../../components/ui/switch";
|
||||||
import { Button } from "~/client/components/ui/button";
|
import { Button } from "~/client/components/ui/button";
|
||||||
|
import { generatePrivateKeyPem } from "~/utils/ssh";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
form: UseFormReturn<FormValues>;
|
form: UseFormReturn<FormValues>;
|
||||||
};
|
};
|
||||||
|
|
||||||
const arrayBufferToBase64 = (buffer: ArrayBuffer) => {
|
|
||||||
let binary = "";
|
|
||||||
const bytes = new Uint8Array(buffer);
|
|
||||||
const chunkSize = 0x8000;
|
|
||||||
|
|
||||||
for (let i = 0; i < bytes.length; i += chunkSize) {
|
|
||||||
binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
|
|
||||||
}
|
|
||||||
|
|
||||||
return btoa(binary);
|
|
||||||
};
|
|
||||||
|
|
||||||
const toPem = (base64: string, label: string) => {
|
|
||||||
const wrapped = base64.match(/.{1,64}/g)?.join("\n") ?? base64;
|
|
||||||
return `-----BEGIN ${label}-----\n${wrapped}\n-----END ${label}-----`;
|
|
||||||
};
|
|
||||||
|
|
||||||
const generatePrivateKeyPem = async () => {
|
|
||||||
const keyPair = await crypto.subtle.generateKey(
|
|
||||||
{
|
|
||||||
name: "RSASSA-PKCS1-v1_5",
|
|
||||||
modulusLength: 4096,
|
|
||||||
publicExponent: new Uint8Array([1, 0, 1]),
|
|
||||||
hash: "SHA-256",
|
|
||||||
},
|
|
||||||
true,
|
|
||||||
["sign", "verify"],
|
|
||||||
);
|
|
||||||
|
|
||||||
const privateKey = await crypto.subtle.exportKey("pkcs8", keyPair.privateKey);
|
|
||||||
return toPem(arrayBufferToBase64(privateKey), "OPENSSH PRIVATE KEY");
|
|
||||||
};
|
|
||||||
|
|
||||||
export const SFTPForm = ({ form }: Props) => {
|
export const SFTPForm = ({ form }: Props) => {
|
||||||
const skipHostKeyCheck = useWatch({ control: form.control, name: "skipHostKeyCheck" });
|
const skipHostKeyCheck = useWatch({ control: form.control, name: "skipHostKeyCheck" });
|
||||||
const [isGeneratingKey, setIsGeneratingKey] = useState(false);
|
const [isGeneratingKey, setIsGeneratingKey] = useState(false);
|
||||||
|
|
|
||||||
32
app/utils/ssh.ts
Normal file
32
app/utils/ssh.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
const arrayBufferToBase64 = (buffer: ArrayBuffer) => {
|
||||||
|
let binary = "";
|
||||||
|
const bytes = new Uint8Array(buffer);
|
||||||
|
const chunkSize = 0x8000;
|
||||||
|
|
||||||
|
for (let i = 0; i < bytes.length; i += chunkSize) {
|
||||||
|
binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
|
||||||
|
}
|
||||||
|
|
||||||
|
return btoa(binary);
|
||||||
|
};
|
||||||
|
|
||||||
|
const toPem = (base64: string, label: string) => {
|
||||||
|
const wrapped = base64.match(/.{1,64}/g)?.join("\n") ?? base64;
|
||||||
|
return `-----BEGIN ${label}-----\n${wrapped}\n-----END ${label}-----`;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const generatePrivateKeyPem = async () => {
|
||||||
|
const keyPair = await crypto.subtle.generateKey(
|
||||||
|
{
|
||||||
|
name: "RSASSA-PKCS1-v1_5",
|
||||||
|
modulusLength: 4096,
|
||||||
|
publicExponent: new Uint8Array([1, 0, 1]),
|
||||||
|
hash: "SHA-256",
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
["sign", "verify"],
|
||||||
|
);
|
||||||
|
|
||||||
|
const privateKey = await crypto.subtle.exportKey("pkcs8", keyPair.privateKey);
|
||||||
|
return toPem(arrayBufferToBase64(privateKey), "OPENSSH PRIVATE KEY");
|
||||||
|
};
|
||||||
Loading…
Reference in a new issue