curve.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package main
  2. import (
  3. "context"
  4. "sync/atomic"
  5. "time"
  6. "git.ali33.ru/fcg-xvii/curve/v2"
  7. "git.ali33.ru/fcg-xvii/curve/v2/tools"
  8. "git.ali33.ru/fcg-xvii/go-tools/containers/concurrent"
  9. "git.ali33.ru/fcg-xvii/go-tools/json"
  10. )
  11. var (
  12. curveStore = &CurveStore{
  13. List: concurrent.NewList(),
  14. cid: int32(0),
  15. }
  16. )
  17. type Curve struct {
  18. *tools.Curve
  19. id int32
  20. }
  21. func (s *Curve) Map() json.Map {
  22. res := s.Curve.Map()
  23. res["id"] = s.id
  24. return res
  25. }
  26. func (s *Curve) MarshalJSON() ([]byte, error) {
  27. return s.Map().JSON(), nil
  28. }
  29. type CurveStore struct {
  30. *concurrent.List
  31. cid int32
  32. }
  33. func (s *CurveStore) APICurveInit(mes *Message) {
  34. mData := mes.Data()
  35. a, check := checkBigInt(mes, "a")
  36. if !check {
  37. return
  38. }
  39. b, check := checkBigInt(mes, "b")
  40. if !check {
  41. return
  42. }
  43. p, check := checkBigInt(mes, "p")
  44. if !check {
  45. return
  46. }
  47. ctx, _ := context.WithDeadline(
  48. context.Background(),
  49. time.Now().Add(time.Second*time.Duration(mData.Int("deadline_seconds", 30))),
  50. )
  51. c, err := curve.NewCurve(a, b, p, ctx)
  52. if err != nil {
  53. mes.SendResponse(messageError(err.Error()))
  54. return
  55. }
  56. cur := &Curve{
  57. Curve: c,
  58. id: atomic.AddInt32(&s.cid, 1),
  59. }
  60. s.PushBack(cur)
  61. mes.SendResponse(json.Map{
  62. "curve": cur.Map(),
  63. })
  64. }
  65. func (s *CurveStore) APICurves(mes *Message) {
  66. curves := s.List.Slice()
  67. mes.SendResponse(json.Map{
  68. "curves": curves,
  69. })
  70. }