Digitally sign
This is the base step for assuring data/document authenticity. Before starting, make sure to choose from one of the supported algorithms that BLOOCK provides.
- Typescript
- Python
- Java
- PHP
- Golang
import {
AuthenticityClient,
KeyClient,
KeyType,
RecordClient,
Signer,
} from '@bloock/sdk';
const keyClient = new KeyClient();
const authenticityClient = new AuthenticityClient();
const recordClient = new RecordClient();
let key = await keyClient.newLocalKey(KeyType.EcP256k);
let signedRecord = await recordClient
.fromString('Hello world')
.withSigner(new Signer(key))
.build();
console.log('Record was signed successfully');
// we can add another signature with a different key
key = await keyClient.newLocalKey(KeyType.EcP256k);
console.log('Adding another signature');
signedRecord = await recordClient
.fromRecord(signedRecord)
.withSigner(new Signer(key))
.build();
console.log('Record was signed successfully');
console.log(`Hash: ${await signedRecord.getHash()}`);
const signatures = await authenticityClient.getSignatures(signedRecord);
signatures.forEach((signature, i) => {
console.log(`Signature ${i + 1}: ${JSON.stringify(signature, null, 4)}`);
});
from bloock.client.authenticity import AuthenticityClient
from bloock.client.record import RecordClient
from bloock.client.key import KeyClient
from bloock.entity.key.key_type import KeyType
from bloock.entity.authenticity.signer import Signer
if __name__ == "__main__":
authenticity_client = AuthenticityClient()
record_client = RecordClient()
key_client = KeyClient()
key = key_client.new_local_key(KeyType.EcP256k)
signed_record = (
record_client.from_string("Hello world").with_signer(Signer(key)).build()
)
print("Record was signed successfully")
# we can add another signature with a different key
key = key_client.new_local_key(KeyType.EcP256k)
print("Adding another signature")
signed_record = (
record_client.from_record(signed_record).with_signer(Signer(key)).build()
)
print("Record was signed successfully")
record_hash = signed_record.get_hash()
print(f"Hash: {record_hash}")
signatures = authenticity_client.get_signatures(signed_record)
for i, signature in enumerate(signatures):
print(f"Signature {i + 1}: {signature}")
import com.bloock.sdk.client.AuthenticityClient;
import com.bloock.sdk.client.KeyClient;
import com.bloock.sdk.client.RecordClient;
import com.bloock.sdk.entity.authenticity.Signer;
import com.bloock.sdk.entity.key.KeyType;
import com.bloock.sdk.entity.key.LocalKey;
import com.bloock.sdk.entity.record.Record;
public class Sign {
public static void main(String[] args) throws Exception {
RecordClient recordClient = new RecordClient();
AuthenticityClient authenticityClient = new AuthenticityClient();
KeyClient keyClient = new KeyClient();
LocalKey localKey = keyClient.newLocalKey(KeyType.EcP256k);
Record signedRecord = recordClient
.fromString("Hello world")
.withSigner(new Signer(localKey))
.build();
System.out.println("Record was signed successfully");
// we can add another signature with a different key
System.out.println("Adding another signature");
localKey = keyClient.newLocalKey(KeyType.EcP256k);
signedRecord = recordClient.fromRecord(signedRecord)
.withSigner(new Signer(localKey))
.build();
System.out.println("Record was signed successfully");
System.out.println("Hash: " + signedRecord.getHash());
authenticityClient
.getSignatures(signedRecord)
.stream()
.forEach(signature -> {
System.out.println("Signature " + signature.getSignature());
});
}
}
<?php
require "./vendor/autoload.php";
use Bloock\Bloock;
use Bloock\Client\AuthenticityClient;
use Bloock\Client\KeyClient;
use Bloock\Client\RecordClient;
use Bloock\Entity\Authenticity\Signer;
use Bloock\Entity\Key\KeyType;
Bloock::$apiKey = getenv("API_KEY") ?: "";
$keyClient = new KeyClient();
$authenticityClient = new AuthenticityClient();
$recordClient = new RecordClient();
$keys = $keyClient->newLocalKey(KeyType::EcP256k);
$signedRecord = $recordClient->fromString("Hello world")
->withSigner(new Signer($keys))
->build();
print "Record was signed successfully\n";
$keys = $keyClient->newLocalKey(KeyType::EcP256k);
print "Adding another signature\n";
$signedRecord = $recordClient->fromRecord($signedRecord)
->withSigner(new Signer($keys))
->build();
print "Record was signed successfully\n";
$hash = $signedRecord->getHash();
print "Hash: $hash\n";
$signatures = $authenticityClient->getSignatures($signedRecord);
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/bloock/bloock-sdk-go/v2/client"
"github.com/bloock/bloock-sdk-go/v2/entity/authenticity"
"github.com/bloock/bloock-sdk-go/v2/entity/key"
)
func main() {
keyClient := client.NewKeyClient()
recordClient := client.NewRecordClient()
authenticityClient := client.NewAuthenticityClient()
localKey, err := keyClient.NewLocalKey(key.EcP256k)
if err != nil {
log.Println(err)
}
signedRecord, err := recordClient.FromString("Hello world").
WithSigner(authenticity.NewSignerWithLocalKey(localKey, nil)).
Build()
if err != nil {
log.Println(err)
}
fmt.Println("Record was signed successfully")
// we can add another signature with a different key
localKey, err = keyClient.NewLocalKey(key.EcP256k)
if err != nil {
log.Println(err)
}
fmt.Println("Adding another signature")
signedRecord, err = recordClient.FromRecord(signedRecord).
WithSigner(authenticity.NewSignerWithLocalKey(localKey, nil)).
Build()
if err != nil {
log.Println(err)
}
fmt.Println("Record was signed successfully")
hash, err := signedRecord.GetHash()
if err != nil {
log.Println(err)
}
fmt.Printf("Hash: %+v" + hash)
signatures, err := authenticityClient.GetSignatures(signedRecord)
if err != nil {
log.Println(err)
}
for i, signature := range signatures {
prettySignature, _ := json.MarshalIndent(signature, "", " ")
fmt.Printf("Signature %d: %s\n", i+1, string(prettySignature))
}
}