400 lines
10 KiB
Go
400 lines
10 KiB
Go
/*
|
|
|--------------------------------------------------------------------------
|
|
| Marker
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
| Name:
|
|
| - Marker
|
|
|
|
|
| Purpose:
|
|
| - Generate unique markers using time, random, and encoded strategies.
|
|
|
|
|
| Guidelines:
|
|
| - Validate marker type before execution.
|
|
| - Maintain a consistent response structure.
|
|
| - Isolate validation and generation workflows.
|
|
| - Handle failures gracefully.
|
|
|
|
|
| Examples:
|
|
| - ObrimMarker("time", config)
|
|
| - ObrimMarker("random", config)
|
|
| - ObrimMarker("encoded", config)
|
|
|
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Credit
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
| Contributor:
|
|
| - Rajon Ahmed
|
|
| - Scionite
|
|
|
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
package marker
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"math/big"
|
|
"strings"
|
|
"time"
|
|
|
|
"go/module/name/essential/visible/service/helper/log"
|
|
"go/module/name/essential/visible/service/helper/retriever"
|
|
"go/module/name/essential/visible/service/helper/status"
|
|
)
|
|
|
|
const (
|
|
obrimMarkerTypeTime = "time"
|
|
obrimMarkerTypeRandom = "random"
|
|
obrimMarkerTypeEncoded = "encoded"
|
|
)
|
|
|
|
type obrimMarkerResponse struct {
|
|
Status bool `json:"status"`
|
|
Code string `json:"code"`
|
|
Payload any `json:"payload"`
|
|
}
|
|
|
|
// Function Name: ObrimMarker
|
|
// Function Purpose: Route marker generation requests and return a standardized marker response.
|
|
func ObrimMarker(markerType string, config map[string]any) map[string]any {
|
|
defer func() {
|
|
if recover() != nil {
|
|
status.ObrimStatus("ERROR", "Marker execution failed.")
|
|
log.ObrimLog("ERROR", "Marker execution panic recovered.")
|
|
}
|
|
}()
|
|
|
|
log.ObrimLog("INIT", "Marker utility initialized.")
|
|
status.ObrimStatus("INFO", "Marker utility initialized.")
|
|
|
|
if err := obrimMarkerValidateType(markerType); err != nil {
|
|
log.ObrimLog("ERROR", err.Error())
|
|
return obrimMarkerBuildResponse(false, "FAILED_INVALID_TYPE", nil)
|
|
}
|
|
|
|
return obrimMarkerRoute(markerType, config)
|
|
}
|
|
|
|
// Function Name: obrimMarkerValidateType
|
|
// Function Purpose: Validate supported marker generation types.
|
|
func obrimMarkerValidateType(markerType string) error {
|
|
switch markerType {
|
|
case obrimMarkerTypeTime, obrimMarkerTypeRandom, obrimMarkerTypeEncoded:
|
|
return nil
|
|
default:
|
|
return fmt.Errorf("unsupported marker type")
|
|
}
|
|
}
|
|
|
|
// Function Name: obrimMarkerRoute
|
|
// Function Purpose: Route execution to the selected marker generation workflow.
|
|
func obrimMarkerRoute(markerType string, config map[string]any) map[string]any {
|
|
switch markerType {
|
|
case obrimMarkerTypeTime:
|
|
return obrimMarkerTime(config)
|
|
case obrimMarkerTypeRandom:
|
|
return obrimMarkerRandom(config)
|
|
case obrimMarkerTypeEncoded:
|
|
return obrimMarkerEncoded(config)
|
|
default:
|
|
return obrimMarkerBuildResponse(false, "FAILED_INVALID_TYPE", nil)
|
|
}
|
|
}
|
|
|
|
// Function Name: obrimMarkerBuildResponse
|
|
// Function Purpose: Build standardized success and error responses.
|
|
func obrimMarkerBuildResponse(statusValue bool, code string, payload any) map[string]any {
|
|
return map[string]any{
|
|
"status": statusValue,
|
|
"code": code,
|
|
"payload": payload,
|
|
}
|
|
}
|
|
|
|
// Function Name: obrimMarkerTime
|
|
// Function Purpose: Generate time-based unique markers.
|
|
func obrimMarkerTime(config map[string]any) map[string]any {
|
|
log.ObrimLog("VALIDATION", "Validating time marker configuration.")
|
|
|
|
epoch, instance, err := obrimMarkerValidateTime(config)
|
|
if err != nil {
|
|
log.ObrimLog("ERROR", err.Error())
|
|
return obrimMarkerBuildResponse(false, "FAILED_VALIDATION", nil)
|
|
}
|
|
|
|
log.ObrimLog("EXECUTION", "Generating time marker.")
|
|
|
|
markerValue := obrimMarkerTimeGenerate(epoch, instance)
|
|
|
|
payload := map[string]any{
|
|
"marker": markerValue,
|
|
"epoch": epoch,
|
|
"instance": instance,
|
|
}
|
|
|
|
status.ObrimStatus("SUCCESS", "Time marker generated.")
|
|
log.ObrimLog("SUCCESS", "Time marker generated successfully.")
|
|
|
|
return obrimMarkerBuildResponse(true, "SUCCESS_TIME_MARKER_GENERATED", payload)
|
|
}
|
|
|
|
// Function Name: obrimMarkerValidateTime
|
|
// Function Purpose: Validate time marker configuration parameters.
|
|
func obrimMarkerValidateTime(config map[string]any) (int64, string, error) {
|
|
// Logic:
|
|
var epochValue int64
|
|
|
|
// Logic:
|
|
var instanceValue string
|
|
|
|
epochRaw, epochExists := config["epoch"]
|
|
if !epochExists {
|
|
return 0, "", fmt.Errorf("epoch is required")
|
|
}
|
|
|
|
switch value := epochRaw.(type) {
|
|
case int64:
|
|
epochValue = value
|
|
case int:
|
|
epochValue = int64(value)
|
|
case float64:
|
|
epochValue = int64(value)
|
|
default:
|
|
return 0, "", fmt.Errorf("invalid epoch")
|
|
}
|
|
|
|
if epochValue < 0 {
|
|
return 0, "", fmt.Errorf("invalid epoch")
|
|
}
|
|
|
|
instanceRaw, instanceExists := config["instance"]
|
|
if !instanceExists {
|
|
return 0, "", fmt.Errorf("instance is required")
|
|
}
|
|
|
|
instanceValue, _ = instanceRaw.(string)
|
|
|
|
if strings.TrimSpace(instanceValue) == "" {
|
|
return 0, "", fmt.Errorf("invalid instance")
|
|
}
|
|
|
|
return epochValue, instanceValue, nil
|
|
}
|
|
|
|
// Function Name: obrimMarkerTimeGenerate
|
|
// Function Purpose: Produce the final time-based marker value.
|
|
func obrimMarkerTimeGenerate(epoch int64, instance string) string {
|
|
// Logic:
|
|
var currentTime int64 = time.Now().UnixMilli()
|
|
|
|
return fmt.Sprintf("%d-%s", currentTime-epoch, instance)
|
|
}
|
|
|
|
// Function Name: obrimMarkerRandom
|
|
// Function Purpose: Generate random collision-resistant markers.
|
|
func obrimMarkerRandom(config map[string]any) map[string]any {
|
|
length, charset, err := obrimMarkerValidateRandom(config)
|
|
if err != nil {
|
|
log.ObrimLog("ERROR", err.Error())
|
|
return obrimMarkerBuildResponse(false, "FAILED_VALIDATION", nil)
|
|
}
|
|
|
|
markerValue, err := obrimMarkerRandomGenerate(length, charset)
|
|
if err != nil {
|
|
log.ObrimLog("ERROR", err.Error())
|
|
return obrimMarkerBuildResponse(false, "FAILED_GENERATION", nil)
|
|
}
|
|
|
|
payload := map[string]any{
|
|
"marker": markerValue,
|
|
"length": length,
|
|
"charset": charset,
|
|
}
|
|
|
|
status.ObrimStatus("SUCCESS", "Random marker generated.")
|
|
log.ObrimLog("SUCCESS", "Random marker generated successfully.")
|
|
|
|
return obrimMarkerBuildResponse(true, "SUCCESS_RANDOM_MARKER_GENERATED", payload)
|
|
}
|
|
|
|
// Function Name: obrimMarkerValidateRandom
|
|
// Function Purpose: Validate random marker configuration parameters.
|
|
func obrimMarkerValidateRandom(config map[string]any) (int, string, error) {
|
|
// Logic:
|
|
var lengthValue int
|
|
|
|
// Logic:
|
|
var charsetValue string
|
|
|
|
lengthRaw, lengthExists := config["length"]
|
|
if !lengthExists {
|
|
return 0, "", fmt.Errorf("length is required")
|
|
}
|
|
|
|
switch value := lengthRaw.(type) {
|
|
case int:
|
|
lengthValue = value
|
|
case int64:
|
|
lengthValue = int(value)
|
|
case float64:
|
|
lengthValue = int(value)
|
|
default:
|
|
return 0, "", fmt.Errorf("invalid length")
|
|
}
|
|
|
|
if lengthValue <= 0 {
|
|
return 0, "", fmt.Errorf("invalid length")
|
|
}
|
|
|
|
charsetRaw, charsetExists := config["charset"]
|
|
if !charsetExists {
|
|
return 0, "", fmt.Errorf("charset is required")
|
|
}
|
|
|
|
charsetValue, _ = charsetRaw.(string)
|
|
|
|
if charsetValue == "" {
|
|
return 0, "", fmt.Errorf("invalid charset")
|
|
}
|
|
|
|
return lengthValue, charsetValue, nil
|
|
}
|
|
|
|
// Function Name: obrimMarkerRandomGenerate
|
|
// Function Purpose: Produce the final random marker value.
|
|
func obrimMarkerRandomGenerate(length int, charset string) (string, error) {
|
|
// Logic:
|
|
var builder strings.Builder
|
|
|
|
for i := 0; i < length; i++ {
|
|
index, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
builder.WriteByte(charset[index.Int64()])
|
|
}
|
|
|
|
return builder.String(), nil
|
|
}
|
|
|
|
// Function Name: obrimMarkerEncoded
|
|
// Function Purpose: Generate deterministic encoded markers.
|
|
func obrimMarkerEncoded(config map[string]any) map[string]any {
|
|
data, length, charset, salt, err := obrimMarkerValidateEncoded(config)
|
|
if err != nil {
|
|
log.ObrimLog("ERROR", err.Error())
|
|
return obrimMarkerBuildResponse(false, "FAILED_VALIDATION", nil)
|
|
}
|
|
|
|
markerValue := obrimMarkerEncodedGenerate(data, length, charset, salt)
|
|
|
|
payload := map[string]any{
|
|
"marker": markerValue,
|
|
"source": data,
|
|
"length": length,
|
|
}
|
|
|
|
status.ObrimStatus("SUCCESS", "Encoded marker generated.")
|
|
log.ObrimLog("SUCCESS", "Encoded marker generated successfully.")
|
|
|
|
return obrimMarkerBuildResponse(true, "SUCCESS_ENCODED_MARKER_GENERATED", payload)
|
|
}
|
|
|
|
// Function Name: obrimMarkerValidateEncoded
|
|
// Function Purpose: Validate encoded marker configuration parameters.
|
|
func obrimMarkerValidateEncoded(config map[string]any) (string, int, string, string, error) {
|
|
// Logic:
|
|
var dataValue string
|
|
|
|
// Logic:
|
|
var lengthValue int
|
|
|
|
// Logic:
|
|
var charsetValue string
|
|
|
|
// Logic:
|
|
var saltValue string
|
|
|
|
dataRaw, dataExists := config["data"]
|
|
if !dataExists {
|
|
return "", 0, "", "", fmt.Errorf("data is required")
|
|
}
|
|
|
|
dataValue, _ = dataRaw.(string)
|
|
|
|
if dataValue == "" {
|
|
return "", 0, "", "", fmt.Errorf("invalid data")
|
|
}
|
|
|
|
lengthRaw, lengthExists := config["length"]
|
|
if !lengthExists {
|
|
return "", 0, "", "", fmt.Errorf("length is required")
|
|
}
|
|
|
|
switch value := lengthRaw.(type) {
|
|
case int:
|
|
lengthValue = value
|
|
case int64:
|
|
lengthValue = int(value)
|
|
case float64:
|
|
lengthValue = int(value)
|
|
default:
|
|
return "", 0, "", "", fmt.Errorf("invalid length")
|
|
}
|
|
|
|
if lengthValue <= 0 {
|
|
return "", 0, "", "", fmt.Errorf("invalid length")
|
|
}
|
|
|
|
charsetRaw, charsetExists := config["charset"]
|
|
if !charsetExists {
|
|
return "", 0, "", "", fmt.Errorf("charset is required")
|
|
}
|
|
|
|
charsetValue, _ = charsetRaw.(string)
|
|
|
|
if charsetValue == "" {
|
|
return "", 0, "", "", fmt.Errorf("invalid charset")
|
|
}
|
|
|
|
if saltRaw, saltExists := config["salt"]; saltExists {
|
|
saltValue, _ = saltRaw.(string)
|
|
}
|
|
|
|
return dataValue, lengthValue, charsetValue, saltValue, nil
|
|
}
|
|
|
|
// Function Name: obrimMarkerEncodedGenerate
|
|
// Function Purpose: Produce the final encoded marker value.
|
|
func obrimMarkerEncodedGenerate(data string, length int, charset string, salt string) string {
|
|
// Logic:
|
|
var hashValue [32]byte = sha256.Sum256([]byte(data + salt))
|
|
|
|
// Logic:
|
|
var encoded string = hex.EncodeToString(hashValue[:])
|
|
|
|
// Logic:
|
|
var builder strings.Builder
|
|
|
|
for i := 0; i < length; i++ {
|
|
index := int(encoded[i%len(encoded)]) % len(charset)
|
|
builder.WriteByte(charset[index])
|
|
}
|
|
|
|
return builder.String()
|
|
}
|
|
|
|
var (
|
|
_ = retriever.ObrimRetriever
|
|
)
|