-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add openssl hmac #655
Merged
Merged
add openssl hmac #655
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"unsafe" | ||
|
||
"github.com/goplus/llgo/c" | ||
"github.com/goplus/llgo/c/openssl" | ||
) | ||
|
||
func main() { | ||
str := "Hello, World!" | ||
key := "123456" | ||
var lenKey = len(key) | ||
|
||
var digest = make([]byte, openssl.EVP_MAX_MD_SIZE) | ||
var digestLen c.Uint | ||
ctx := openssl.NewHMAC_CTX() | ||
if ctx == nil { | ||
c.Fprintf(c.Stderr, c.Str("%s\n"), c.Str("Error creating HMAC_CTX")) | ||
return | ||
} | ||
defer ctx.Free() | ||
|
||
var ret c.Int = ctx.InitEx(unsafe.Pointer(unsafe.StringData(key)), c.Int(lenKey), openssl.EVP_sha256(), nil) | ||
if ret == 0 { | ||
c.Fprintf(c.Stderr, c.Str("%s\n"), c.Str("Error initializing HMAC_CTX")) | ||
return | ||
} | ||
ret = ctx.UpdateString(str) | ||
if ret == 0 { | ||
c.Fprintf(c.Stderr, c.Str("%s\n"), c.Str("Error updating HMAC_CTX")) | ||
return | ||
} | ||
ret = ctx.Final(unsafe.SliceData(digest), &digestLen) | ||
if ret == 0 { | ||
c.Fprintf(c.Stderr, c.Str("%s\n"), c.Str("Error finalizing HMAC_CTX")) | ||
return | ||
} | ||
fmt.Print("HMAC: ") | ||
for i := 0; i < int(digestLen); i++ { | ||
fmt.Printf("%02x", digest[i]) | ||
} | ||
fmt.Print("\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package openssl | ||
|
||
import ( | ||
"unsafe" | ||
|
||
"github.com/goplus/llgo/c" | ||
) | ||
|
||
const EVP_MAX_MD_SIZE = 64 /* longest known is SHA512 */ | ||
|
||
// const EVP_MD *EVP_sha1(void) | ||
// | ||
//go:linkname EVP_sha1 C.EVP_sha1 | ||
func EVP_sha1() *EVP_MD | ||
|
||
// const EVP_MD *EVP_sha224(void) | ||
// | ||
//go:linkname EVP_sha224 C.EVP_sha224 | ||
func EVP_sha224() *EVP_MD | ||
|
||
// func EVP_sha256() *EVP_MD | ||
// | ||
//go:linkname EVP_sha256 C.EVP_sha256 | ||
func EVP_sha256() *EVP_MD | ||
|
||
// const EVP_MD *EVP_sha512_224(void) | ||
// | ||
//go:linkname EVP_sha512_224 C.EVP_sha512_224 | ||
func EVP_sha512_224() *EVP_MD | ||
|
||
// const EVP_MD *EVP_sha512_256(void) | ||
// | ||
//go:linkname EVP_sha512_256 C.EVP_sha512_256 | ||
func EVP_sha512_256() *EVP_MD | ||
|
||
// const EVP_MD *EVP_sha384(void) | ||
// | ||
//go:linkname EVP_sha384 C.EVP_sha384 | ||
func EVP_sha384() *EVP_MD | ||
|
||
// const EVP_MD *EVP_sha512(void) | ||
// | ||
//go:linkname EVP_sha512 C.EVP_sha512 | ||
func EVP_sha512() *EVP_MD | ||
|
||
type EVP_MD struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个类放到它的所有实例创建函数前更好。 |
||
Unused [0]byte | ||
} | ||
|
||
type EVP_MD_CTX struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个没有用到 |
||
Unused [0]byte | ||
} | ||
|
||
type HMAC_CTX struct { | ||
Unused [0]byte | ||
} | ||
|
||
// OSSL_DEPRECATEDIN_3_0 HMAC_CTX *HMAC_CTX_new(void); | ||
// | ||
//go:linkname NewHMAC_CTX C.HMAC_CTX_new | ||
func NewHMAC_CTX() *HMAC_CTX | ||
|
||
// OSSL_DEPRECATEDIN_3_0 void HMAC_CTX_free(HMAC_CTX *ctx); | ||
// | ||
// llgo:link (*HMAC_CTX).Free C.HMAC_CTX_free | ||
func (c *HMAC_CTX) Free() {} | ||
|
||
// OSSL_DEPRECATEDIN_3_0 size_t HMAC_size(const HMAC_CTX *e); | ||
// | ||
// llgo:link (*HMAC_CTX).Size C.HMAC_size | ||
func (c *HMAC_CTX) Size() uintptr { return 0 } | ||
|
||
// OSSL_DEPRECATEDIN_3_0 int HMAC_CTX_reset(HMAC_CTX *ctx); | ||
// | ||
// llgo:link (*HMAC_CTX).Reset C.HMAC_CTX_reset | ||
func (c *HMAC_CTX) Reset() c.Int { return 0 } | ||
|
||
// OSSL_DEPRECATEDIN_1_1_0 __owur int HMAC_Init(HMAC_CTX *ctx, | ||
// const void *key, int len, | ||
// const EVP_MD *md); | ||
// | ||
// llgo:link (*HMAC_CTX).Init C.HMAC_Init | ||
func (c *HMAC_CTX) Init(key unsafe.Pointer, len c.Int, md *EVP_MD) c.Int { return 0 } | ||
|
||
// OSSL_DEPRECATEDIN_3_0 int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, | ||
// const EVP_MD *md, ENGINE *impl); | ||
// | ||
// llgo:link (*HMAC_CTX).InitEx C.HMAC_Init_ex | ||
func (c *HMAC_CTX) InitEx(key unsafe.Pointer, len c.Int, md *EVP_MD, impl unsafe.Pointer) c.Int { | ||
return 0 | ||
} | ||
|
||
// OSSL_DEPRECATEDIN_3_0 int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, | ||
// size_t len); | ||
// | ||
// llgo:link (*HMAC_CTX).Update C.HMAC_Update | ||
func (c *HMAC_CTX) Update(data unsafe.Pointer, len uintptr) c.Int { return 0 } | ||
|
||
func (c *HMAC_CTX) UpdateBytes(data []byte) c.Int { | ||
return c.Update(unsafe.Pointer(unsafe.SliceData(data)), uintptr(len(data))) | ||
} | ||
|
||
func (c *HMAC_CTX) UpdateString(data string) c.Int { | ||
return c.Update(unsafe.Pointer(unsafe.StringData(data)), uintptr(len(data))) | ||
} | ||
|
||
// OSSL_DEPRECATEDIN_3_0 int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, | ||
// unsigned int *len); | ||
// | ||
// llgo:link (*HMAC_CTX).Final C.HMAC_Final | ||
func (c *HMAC_CTX) Final(md *byte, len *c.Uint) c.Int { return 0 } | ||
|
||
// OSSL_DEPRECATEDIN_3_0 __owur int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx); | ||
// | ||
// llgo:link (*HMAC_CTX).Copy C.HMAC_CTX_copy | ||
func (c *HMAC_CTX) Copy(sctx *HMAC_CTX) c.Int { return 0 } | ||
|
||
// OSSL_DEPRECATEDIN_3_0 void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags); | ||
// | ||
// llgo:link (*HMAC_CTX).SetFlags C.HMAC_CTX_set_flags | ||
func (c *HMAC_CTX) SetFlags(flags c.Ulong) {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用
fmt.Printf("%x\n", digest[:digestLen])
就行