digest.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (C) 2017. See AUTHORS.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package openssl
  15. // #include "shim.h"
  16. import "C"
  17. import (
  18. "fmt"
  19. "unsafe"
  20. )
  21. // Digest represents and openssl message digest.
  22. type Digest struct {
  23. ptr *C.EVP_MD
  24. }
  25. // GetDigestByName returns the Digest with the name or nil and an error if the
  26. // digest was not found.
  27. func GetDigestByName(name string) (*Digest, error) {
  28. cname := C.CString(name)
  29. defer C.free(unsafe.Pointer(cname))
  30. p := C.X_EVP_get_digestbyname(cname)
  31. if p == nil {
  32. return nil, fmt.Errorf("Digest %v not found", name)
  33. }
  34. // we can consider digests to use static mem; don't need to free
  35. return &Digest{ptr: p}, nil
  36. }
  37. // GetDigestByName returns the Digest with the NID or nil and an error if the
  38. // digest was not found.
  39. func GetDigestByNid(nid NID) (*Digest, error) {
  40. sn, err := Nid2ShortName(nid)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return GetDigestByName(sn)
  45. }