Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. defp signature(signing_key, string_to_sign) do
  2. hmac_sha256(signing_key, string_to_sign)
  3. |> bytes_to_string
  4. end
  5.  
  6. defp signing_key(secret_key, date, region) do
  7. hmac_sha256("AWS4#{secret_key}", date)
  8. |> hmac_sha256(region)
  9. |> hmac_sha256(@service)
  10. |> hmac_sha256(@aws_request)
  11. end
  12.  
  13. def hmac_sha256(key, data) do
  14. :crypto.hmac(:sha256, key, data)
  15. end
  16.  
  17. def bytes_to_string(bytes) do
  18. Base.encode16(bytes, case: :lower)
  19. end
  20.  
  21. defp policy(key, mimetype, credential, date, expiration_window \ 60) do
  22. %{
  23. expiration: now_plus(expiration_window),
  24. conditions: [
  25. %{bucket: bucket_name},
  26. ["starts-with", "$key", key],
  27. %{acl: "public-read"},
  28. %{success_action_status: "201"},
  29. ["starts-with", "$Content-Type", mimetype],
  30. %{"x-amz-credential": credential},
  31. %{"x-amz-algorithm": "AWS4-HMAC-SHA256"},
  32. %{"x-amz-date": date}
  33. ]
  34. }
  35. |> Poison.encode!
  36. |> Base.encode64
  37. end
  38.  
  39. defp credential(date) do
  40. credential(aws_config[:access_key_id], date)
  41. end
  42.  
  43. defp credential(key, date) do
  44. key <> "/" <> date <> "/" <> region() <> "/" <> @service <> "/" <> @aws_request
  45. end
  46.  
  47. makeMultiPart : UploadSignatureModel -> File -> Http.Body
  48. makeMultiPart uploadSignature file =
  49. Http.multipartBody
  50. [ Http.stringPart "key" uploadSignature.key
  51. , Http.stringPart "acl" uploadSignature.acl
  52. , Http.stringPart "success_action_status" "201"
  53. , Http.stringPart "Content-Type" uploadSignature.content_type
  54. , Http.stringPart "X-Amz-Credential" uploadSignature.credential
  55. , Http.stringPart "X-Amz-Algorithm" "AWS4-HMAC-SHA256"
  56. , Http.stringPart "Policy" uploadSignature.policy
  57. , Http.stringPart "Signature" uploadSignature.signature
  58. , Http.stringPart "AWSAccessKeyId" uploadSignature.aws_access_key_id
  59. , Http.filePart "file" file
  60. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement