1.4.5.2
This commit is contained in:
115
EdgeNode/internal/js/lib_crypto_hmac.go
Normal file
115
EdgeNode/internal/js/lib_crypto_hmac.go
Normal file
@@ -0,0 +1,115 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build script
|
||||
// +build script
|
||||
|
||||
package js
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
"errors"
|
||||
"fmt"
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
"hash"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if !teaconst.IsMain {
|
||||
return
|
||||
}
|
||||
|
||||
SharedLibraryManager.Register(&JSHMACCryptoLibrary{})
|
||||
}
|
||||
|
||||
type JSHMACCryptoLibrary struct {
|
||||
JSBaseLibrary
|
||||
}
|
||||
|
||||
func (this *JSHMACCryptoLibrary) JSNamespace() string {
|
||||
return "gojs.crypto.JSHMACLibrary"
|
||||
}
|
||||
|
||||
func (this *JSHMACCryptoLibrary) JSPrototype() string {
|
||||
return `gojs.crypto.HMAC = class {
|
||||
#goObjectId
|
||||
|
||||
constructor(algorithm, key) {
|
||||
this.#goObjectId = $this.NewHMAC(algorithm, key)
|
||||
}
|
||||
|
||||
update(data) {
|
||||
$this.Update(this.#goObjectId, data)
|
||||
}
|
||||
|
||||
sum() {
|
||||
return $this.Sum(this.#goObjectId)
|
||||
}
|
||||
}`
|
||||
}
|
||||
|
||||
func (this *JSHMACCryptoLibrary) NewHMAC(arguments *FunctionArguments) (any, error) {
|
||||
algorithm, ok := arguments.StringAt(0)
|
||||
if !ok {
|
||||
return nil, errors.New("require hash algorithm")
|
||||
}
|
||||
|
||||
key, ok := arguments.StringAt(1)
|
||||
if !ok {
|
||||
return nil, errors.New("require hash key")
|
||||
}
|
||||
|
||||
var h hash.Hash
|
||||
switch algorithm {
|
||||
case "md5":
|
||||
h = hmac.New(md5.New, []byte(key))
|
||||
case "sha1":
|
||||
h = hmac.New(sha1.New, []byte(key))
|
||||
case "sha256":
|
||||
h = hmac.New(sha256.New, []byte(key))
|
||||
case "sha512":
|
||||
h = hmac.New(sha512.New, []byte(key))
|
||||
default:
|
||||
return nil, errors.New("invalid hash algorithm")
|
||||
}
|
||||
|
||||
return arguments.Context().AddGoObject(h), nil
|
||||
}
|
||||
|
||||
func (this *JSHMACCryptoLibrary) Update(arguments *FunctionArguments) (any, error) {
|
||||
obj, ok := arguments.GoObjectAt(0)
|
||||
if !ok {
|
||||
return nil, ErrGoObjectNotFound
|
||||
}
|
||||
|
||||
data, ok := arguments.FormatStringAt(1)
|
||||
if !ok {
|
||||
return nil, errors.New("invalid data")
|
||||
}
|
||||
|
||||
h, ok := obj.(hash.Hash)
|
||||
if !ok {
|
||||
return nil, errors.New("invalid hash")
|
||||
}
|
||||
|
||||
h.Reset()
|
||||
h.Write([]byte(data))
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (this *JSHMACCryptoLibrary) Sum(arguments *FunctionArguments) (any, error) {
|
||||
obj, ok := arguments.GoObjectAt(0)
|
||||
if !ok {
|
||||
return nil, ErrGoObjectNotFound
|
||||
}
|
||||
|
||||
h, ok := obj.(hash.Hash)
|
||||
if !ok {
|
||||
return nil, errors.New("invalid hash")
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%x", h.Sum(nil)), nil
|
||||
}
|
||||
Reference in New Issue
Block a user