feat: update deno-sshsig-mod.ts

This commit is contained in:
2025-01-11 16:46:35 +08:00
parent 0a501bbef6
commit 9fc3613cf5

View File

@@ -1,7 +1,7 @@
import {crypto} from "jsr:@std/crypto"; import {crypto} from "jsr:@std/crypto";
import {decodeBase64} from "jsr:@std/encoding/base64"; import {decodeBase64} from "jsr:@std/encoding/base64";
import {encodeBase64Url} from "jsr:@std/encoding/base64url"; import {encodeBase64Url} from "jsr:@std/encoding/base64url";
import {encodeHex} from "jsr:@std/encoding/hex"; import {decodeHex, encodeHex} from "jsr:@std/encoding/hex";
// Reference: // Reference:
// * https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.sshsig // * https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.sshsig
@@ -66,8 +66,8 @@ class BinaryWriter {
} }
writeString(byte: Uint8Array) { writeString(byte: Uint8Array) {
this._writeUint32(byte.byteLength); this.writeUint32(byte.byteLength);
this._writeBytes(byte); this.writeBytes(byte);
} }
writeStringFromString(str: string) { writeStringFromString(str: string) {
@@ -77,16 +77,22 @@ class BinaryWriter {
writeLengthFromString(str: string) { writeLengthFromString(str: string) {
const bytes = new TextEncoder().encode(str); const bytes = new TextEncoder().encode(str);
this._writeBytes(bytes); this.writeBytes(bytes);
} }
_writeUint32(num: number) { writeUint32(num: number) {
let dataView = new DataView(new ArrayBuffer(4), 0); let dataView = new DataView(new ArrayBuffer(4), 0);
dataView.setUint32(0, num); dataView.setUint32(0, num);
this._writeBytes(new Uint8Array(dataView.buffer)); this.writeBytes(new Uint8Array(dataView.buffer));
} }
_writeBytes(bytes: Uint8Array) { writeNumber(num: number) {
let n = new Uint8Array(1)
n[0] = num;
this.writeBytes(n);
}
writeBytes(bytes: Uint8Array) {
this.buffers.push(bytes); this.buffers.push(bytes);
} }
} }
@@ -203,6 +209,33 @@ class SshSignatureValue {
const s = signatureEcReader.readString(); const s = signatureEcReader.readString();
return new SshSignatureValue(signatureAlgorithm, r, s); return new SshSignatureValue(signatureAlgorithm, r, s);
} }
// SEQUENCE {
// INTEGER: 00cdf3f5e083974961a9737daa2352cf08f7e652f103d4e3cc494b5ffadccf1a6d
// INTEGER: 69658b75c9c7523c15e6de16907350f0d0fd51114237c3e32bdd9fe92465e768
// }
toDer(): Uint8Array {
let writer = new BinaryWriter();
writer.writeNumber(0x30);
writer.writeNumber(0x45);
writer.writeNumber(2);
const rFirstByte = this.ecSignatureR[0];
writer.writeNumber(((rFirstByte >= 0x80) ? 1 : 0) + this.ecSignatureR.byteLength);
if (rFirstByte >= 0x80) {
writer.writeNumber(0);
}
writer.writeBytes(this.ecSignatureR);
writer.writeNumber(2);
const sFirstByte = this.ecSignatureS[0];
writer.writeNumber(((sFirstByte >= 0x80) ? 1 : 0) + this.ecSignatureS.byteLength);
if (sFirstByte >= 0x80) {
writer.writeNumber(0);
}
writer.writeBytes(this.ecSignatureS);
return writer.merge();
}
} }
class SshPublicKey { class SshPublicKey {
@@ -331,6 +364,7 @@ console.log(encodeHex(sig));
const publicKey = await sshSignature.publicKey.importJwk(); const publicKey = await sshSignature.publicKey.importJwk();
console.log(publicKey); console.log(publicKey);
console.log(encodeHex(sshSignature.signature.toDer()));
// crypto.subtle.verify( // crypto.subtle.verify(
// { // {