Generating deterministic ECDSA keys based on seed and salt using zero-dependencies JavaScript and SubtleCrypto

Update 1 – posting the code to test the solution.

Given seed and salt how do I produce a set of deterministic ECDSA keys using zero-dependencies JavaScript and SubtleCrypto?
This is a continuation of this question, which I was advised to form as a new question. Therefore I publish no sample code, my understanding is that there’re 2 major ways to go about it:

  1. Generate the key material and then somehow import it as ECDSA keys.
  2. Generate x & y required to generate ECDSA keys (I’m not sure how, given the generateKey call accepting just named curve, maybe via jwk(?))

I’d like the solution to rely as much as possibly on SubtleCrypto API with as little custom code as possible.

The code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>async function generateDeterministicECDSAKey(seed, salt) {
// Step 1: Convert seed and salt to ArrayBuffers
const enc = new TextEncoder();
const seedBuffer = enc.encode(seed); // Seed as a buffer
const saltBuffer = enc.encode(salt); // Salt as a buffer
// Step 2: Derive key material using PBKDF2
const baseKey = await crypto.subtle.importKey(
'raw',
seedBuffer,
{ name: 'PBKDF2' },
false,
['deriveBits', 'deriveKey']
);
// Derive 256 bits (32 bytes) of key material for the private key
const derivedBits = await crypto.subtle.deriveBits(
{
name: 'PBKDF2',
salt: saltBuffer, // Salt for PBKDF2
iterations: 100000, // Use a secure iteration count
hash: 'SHA-256' // Hash function for PBKDF2
},
baseKey,
256 // 256 bits for the derived key
);
// Step 3: Import derived key material as ECDSA private key
// NOTE - that's where it fails due to a mismatch between the "raw" format
// and { name: 'ECDSA', namedCurve: 'P-256' } algo
const privateKey = await crypto.subtle.importKey(
'raw',
derivedBits, // Use derived bits as private key
{ name: 'ECDSA', namedCurve: 'P-256' }, // ECDSA using the P-256 curve
true, // Mark key as exportable
['sign'] // Key usage for signing
);
// Step 4: Export the private key in PKCS8 format (optional)
const pkcs8Key = await crypto.subtle.exportKey('pkcs8', privateKey);
// Step 5: Generate public key pair (optional)
const publicKey = await crypto.subtle.exportKey('jwk', privateKey);
return {
privateKey: privateKey,
pkcs8Key: pkcs8Key,
publicKey: publicKey
};
}
// Helper function to convert ArrayBuffer to base64 string (for display)
function arrayBufferToBase64(buffer) {
const binary = String.fromCharCode.apply(null, new Uint8Array(buffer));
return window.btoa(binary);
}
// Example usage
const seed = "your_deterministic_seed"; // Example seed
const salt = "your_salt_value"; // Example salt
await generateDeterministicECDSAKey(seed, salt).then(keys => {
const privateKeyBase64 = arrayBufferToBase64(keys.pkcs8Key);
console.log("Private Key (PKCS8 Base64):", privateKeyBase64);
console.log("Public Key (JWK):", keys.publicKey);
});
</code>
<code>async function generateDeterministicECDSAKey(seed, salt) { // Step 1: Convert seed and salt to ArrayBuffers const enc = new TextEncoder(); const seedBuffer = enc.encode(seed); // Seed as a buffer const saltBuffer = enc.encode(salt); // Salt as a buffer // Step 2: Derive key material using PBKDF2 const baseKey = await crypto.subtle.importKey( 'raw', seedBuffer, { name: 'PBKDF2' }, false, ['deriveBits', 'deriveKey'] ); // Derive 256 bits (32 bytes) of key material for the private key const derivedBits = await crypto.subtle.deriveBits( { name: 'PBKDF2', salt: saltBuffer, // Salt for PBKDF2 iterations: 100000, // Use a secure iteration count hash: 'SHA-256' // Hash function for PBKDF2 }, baseKey, 256 // 256 bits for the derived key ); // Step 3: Import derived key material as ECDSA private key // NOTE - that's where it fails due to a mismatch between the "raw" format // and { name: 'ECDSA', namedCurve: 'P-256' } algo const privateKey = await crypto.subtle.importKey( 'raw', derivedBits, // Use derived bits as private key { name: 'ECDSA', namedCurve: 'P-256' }, // ECDSA using the P-256 curve true, // Mark key as exportable ['sign'] // Key usage for signing ); // Step 4: Export the private key in PKCS8 format (optional) const pkcs8Key = await crypto.subtle.exportKey('pkcs8', privateKey); // Step 5: Generate public key pair (optional) const publicKey = await crypto.subtle.exportKey('jwk', privateKey); return { privateKey: privateKey, pkcs8Key: pkcs8Key, publicKey: publicKey }; } // Helper function to convert ArrayBuffer to base64 string (for display) function arrayBufferToBase64(buffer) { const binary = String.fromCharCode.apply(null, new Uint8Array(buffer)); return window.btoa(binary); } // Example usage const seed = "your_deterministic_seed"; // Example seed const salt = "your_salt_value"; // Example salt await generateDeterministicECDSAKey(seed, salt).then(keys => { const privateKeyBase64 = arrayBufferToBase64(keys.pkcs8Key); console.log("Private Key (PKCS8 Base64):", privateKeyBase64); console.log("Public Key (JWK):", keys.publicKey); }); </code>
async function generateDeterministicECDSAKey(seed, salt) {
    // Step 1: Convert seed and salt to ArrayBuffers
    const enc = new TextEncoder();
    const seedBuffer = enc.encode(seed); // Seed as a buffer
    const saltBuffer = enc.encode(salt); // Salt as a buffer

    // Step 2: Derive key material using PBKDF2
    const baseKey = await crypto.subtle.importKey(
        'raw',
        seedBuffer,
        { name: 'PBKDF2' },
        false,
        ['deriveBits', 'deriveKey']
    );

    // Derive 256 bits (32 bytes) of key material for the private key
    const derivedBits = await crypto.subtle.deriveBits(
        {
            name: 'PBKDF2',
            salt: saltBuffer,  // Salt for PBKDF2
            iterations: 100000, // Use a secure iteration count
            hash: 'SHA-256'     // Hash function for PBKDF2
        },
        baseKey,
        256  // 256 bits for the derived key
    );

    // Step 3: Import derived key material as ECDSA private key
    // NOTE - that's where it fails due to a mismatch between the "raw" format
    // and  { name: 'ECDSA', namedCurve: 'P-256' } algo
    const privateKey = await crypto.subtle.importKey(
        'raw',
        derivedBits,  // Use derived bits as private key
        { name: 'ECDSA', namedCurve: 'P-256' },  // ECDSA using the P-256 curve
        true,  // Mark key as exportable
        ['sign']  // Key usage for signing
    );

    // Step 4: Export the private key in PKCS8 format (optional)
    const pkcs8Key = await crypto.subtle.exportKey('pkcs8', privateKey);
    
    // Step 5: Generate public key pair (optional)
    const publicKey = await crypto.subtle.exportKey('jwk', privateKey);

    return {
        privateKey: privateKey,
        pkcs8Key: pkcs8Key,
        publicKey: publicKey
    };
}

// Helper function to convert ArrayBuffer to base64 string (for display)
function arrayBufferToBase64(buffer) {
    const binary = String.fromCharCode.apply(null, new Uint8Array(buffer));
    return window.btoa(binary);
}

// Example usage
const seed = "your_deterministic_seed"; // Example seed
const salt = "your_salt_value";         // Example salt
await generateDeterministicECDSAKey(seed, salt).then(keys => {
    const privateKeyBase64 = arrayBufferToBase64(keys.pkcs8Key);
    console.log("Private Key (PKCS8 Base64):", privateKeyBase64);
    console.log("Public Key (JWK):", keys.publicKey);
});

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật